Reputation: 3
I have this code: Please help me on how to get user input for the equation.
from __future__ import division
pi = 3.14159265
g = 6.67428*(10**-11)
radius = raw_input("Enter Radius -->")
def display_results(radius , mass , velocity):
print "Radius of the planet" , radius/1000 ,"km"
print "Mass of the planet" , float(mass/10**21) ,"(10**21 kg)"
print "Escape velocity of the planet" , velocity/1000 , "(km/s)"
def escape_velocity(circumference , acceleration):
radius = circumference/(2*pi)
mass = (acceleration * radius ** 2)/g
vEscape = ((2*g*mass)/radius)**0.5
display_results(radius , mass , vEscape)
escape_velocity(40075000 , 10)
Here is what I am supposed to do: Use effective mainline logic to get user input and then call the escape_velocity() function to calculate and display the final results. Below is what a sample run from your program should look like (** ** text is example input from a user):
Circumference (km) of planet? **38000**
Acceleration due to gravity (m/s^2)? **9.8**
Calculating the escape velocity...
Planet radius = 6047.9 km
Planet mass = 5370.7 x 10^21 kg
Escape velocity = 10.9 km/s
How do I make the user input a number so that my program will solve the equation. I need to have user input:
Circumference (km) of planet?
Acceleration due to gravity (m/s^2)?
Thanks a lot!!
Upvotes: 0
Views: 3660
Reputation:
You need to convert the radius into integer. Because the raw_input function gives the output in string value, hence you can not divide str(radius) by number 1000. You just need to write like this radius = raw_input("Enter Radius -->") radius = int(radius) Hope this will help you :)
You can check out more about raw_input here
Upvotes: 0
Reputation: 56
I got it to ask for user input as you asked and display essentially your desired result with the following code:
from __future__ import division
import math
pi = 3.14159265
g = 6.67428*(10**-11)
#radius = raw_input("Enter Radius -->")
user_circum = raw_input("Circumference (km) of planet? ")
user_acc = raw_input("Acceleration due to gravity (m/s^2)?")
def display_results(radius , mass , velocity):
print "Radius of the planet" , radius ,"km"
print "Mass of the planet" , float(mass/10**15) ,"(10^21 kg)"
print "Escape velocity of the planet" , velocity/1000 , "(km/s)"
def escape_velocity(circumference , acceleration):
circumference = float(circumference)
acceleration = float(acceleration)
radius = circumference/(2*pi)
mass = (acceleration * radius ** 2)/g
vEscape = ((2*g*mass)/radius)**0.5
display_results(radius , mass , vEscape)
escape_velocity(user_circum, user_acc)
However, some of the math in your equation calculation appeared to be a little off. I'd double-check those equations, but you appear to be pretty set! Hope this helps.
Upvotes: 2
Reputation: 396
You already have the syntax for getting a user to input a number and assign it to the variable radius
(the raw_input
line).
You use the same syntax to ask for circumference and acceleration inputs, then call the escape velocity function with the two variables that received the user input as arguments. (In your example code this function is being called with two integers as arguments, (40075000 , 10), so you need to change that.)
Upvotes: 0