## Problem with simple program

This is the place for queries that don't fit in any of the other categories.

### Problem with simple program

I am new to python just started today and I was playing around and decided to make a program that takes inputs and calculates the results. I am having trouble with the sqaure root function not returning correct values here is my code:
Code: Select all
`import mathprint "Which equation will you use?"print "A = newtons gravitational equation, F=GMm/r*r"print "B = Centripetal acceleration a=v*v/r"print "C = Keplar's equation for period T*T=((4*pi*r*r*r)/GM)"G = (6.67e-11)g = 9.81pi = 3.14159265359equation = raw_input()print "You chose equation %r" % (equation)if equation in ['A', 'a']:   print "Value for M?"   M = float(raw_input())   print "Value for m?"   m = float(raw_input())   print "Value for r?"   r = float(raw_input())   print "The grativational force acting is:", ((G*M*m)/(r*r))if equation in ['B', 'b']:   print "Value for v?"   V = float(raw_input())   print "Value for r?"   r = float(raw_input())   print "the centripetal acceleration is:",((V*V)/r)if equation in ['C', 'c']:   print "Value for r?"   r = float(raw_input())   print "Value for M"   M = float(raw_input())   print "The period of orbit is:", math.sqrt((4*pi*r*r*r)/G*M)   `
Last edited by micseydel on Thu Oct 24, 2013 6:00 pm, edited 1 time in total.
Reason: Locked OP.
johnahh

Posts: 4
Joined: Thu Oct 24, 2013 4:48 pm

### Re: Problem with simple program

You're going to have to be a great deal more specific than
johnahh wrote:I am having trouble with the sqaure root function not returning correct values here is my code:

Your program has I/O, and you're asking about only one of the three if blocks. Reproduce your problem with as little code as possible and no raw_input() calls (just hard code values that show the problem). You should not need more than 10 lines, and it looks like even less here. State what result you get as well as what you expect.
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.

micseydel

Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

### Re: Problem with simple program

Code: Select all
`# variables used for calculationimport mathG = 6.67e-11M = 6.0e24r = 1.5e11pi = 3.14159265359# display valueprint "T=", math.sqrt(4*pi*pi*r*r*r/G*M)`

This is the code needed for this calculation, using my calculator i get an answer of 1.82e10
Using this code i obtain 1.09e35 a magnitude of 25 orders out.
johnahh

Posts: 4
Joined: Thu Oct 24, 2013 4:48 pm

### Re: Problem with simple program

Python uses the same math rules as humans do. You're just writing the formula wrong.

The formula you're using right now(inside math.sqrt()):
incorrect formula
incorrect_formula.jpg (2.56 KiB) Viewed 1331 times

The formula you want:
correct formula
correct_formula.jpg (2.46 KiB) Viewed 1331 times

This is written in python as:
Code: Select all
`4 * pi**2 * r**3 / (G * M)`

Note my use of the power operator **.
Friendship is magic!

R.I.P. Tracy M. You will be missed.

stranac

Posts: 1790
Joined: Thu Feb 07, 2013 3:42 pm

### Re: Problem with simple program

thank you stranac, i soon after posting realised what i had done but i was near not a computer to edit my post.
Thank you for showing the power operator, i was unaware of this.
Is there a way a can print with a superscript 2 to denote sqaured? when asking what equation to use it looks very messy with r*r or r**3.
johnahh

Posts: 4
Joined: Thu Oct 24, 2013 4:48 pm

### Re: Problem with simple program

You could use the pow builtin if it better suits your aesthetics.

Code: Select all
`>>> x=5>>> pow(x,3)125`

-Mek
New Users, Read This
• Use code tags when posting code.
• Include any errors with your post (in code tags).
• Describe your problem; not your chosen solution.
• Make examples the minimum length to demonstrate your issue.

Mekire

Posts: 1711
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona

### Re: Problem with simple program

Thanks for the reply mekire but i mean when asking what equation to select it will be letters squared so a value is not needed.i guess id have to make a GUI to make it look nice. too advanced for me
johnahh

Posts: 4
Joined: Thu Oct 24, 2013 4:48 pm