0

我有这个代码:请帮助我了解如何获取用户对方程式的输入。

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)

这是我应该做的:使用有效的主线逻辑来获取用户输入,然后调用 escape_velocity() 函数来计算并显示最终结果。下面是从您的程序运行的示例应该是什么样子(** ** 文本是来自用户的示例输入):

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

如何让用户输入一个数字,以便我的程序求解方程。我需要有用户输入:

Circumference (km) of planet? 
Acceleration due to gravity (m/s^2)?

非常感谢!!

4

3 回答 3

2

我得到它来要求用户输入,并使用以下代码基本上显示您想要的结果:

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)

但是,您的方程式计算中的一些数学运算似乎有些偏差。我会仔细检查这些方程式,但你似乎很确定!希望这可以帮助。

于 2013-10-16T22:52:46.223 回答
0

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.)

于 2013-10-16T22:49:53.647 回答
0

您需要将半径转换为整数。因为 raw_input 函数以字符串值给出输出,因此你不能将 str(radius) 除以数字 1000。你只需要这样写 radius = raw_input("Enter Radius -->") radius = int(radius) Hope这会帮助你:)

您可以在此处查看有关 raw_input 的更多信息

于 2013-10-17T05:45:21.060 回答