Problem with simple program

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

Problem with simple program

Postby johnahh » Thu Oct 24, 2013 4:58 pm

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 math
print "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.81
pi = 3.14159265359
equation = 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

Postby micseydel » Thu Oct 24, 2013 6:19 pm

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.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1354
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Problem with simple program

Postby johnahh » Fri Oct 25, 2013 11:37 am

Code: Select all
# variables used for calculation
import math
G = 6.67e-11
M = 6.0e24
r = 1.5e11
pi = 3.14159265359
# display value
print "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

Postby stranac » Fri Oct 25, 2013 12:30 pm

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.jpg
incorrect formula
incorrect_formula.jpg (2.56 KiB) Viewed 269 times


The formula you want:
correct_formula.jpg
correct formula
correct_formula.jpg (2.46 KiB) Viewed 269 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.
User avatar
stranac
 
Posts: 1143
Joined: Thu Feb 07, 2013 3:42 pm

Re: Problem with simple program

Postby johnahh » Fri Oct 25, 2013 2:59 pm

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

Postby Mekire » Fri Oct 25, 2013 3:22 pm

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

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

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Problem with simple program

Postby johnahh » Fri Oct 25, 2013 8:31 pm

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


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests

cron