如何从用户那里获取输入并在python中应用计算?

How to get input from user and apply calculation in Python?

本问题已经有最佳答案,请猛点这里访问。

我刚开始用Python。

我想从用户那里得到输入并计算它。

例:我想用t =(v sin(theta))/g得到弹丸运动的时间飞行。

1
2
3
4
5
6
import math
print"this program will find time flight of projectile motion"
g = 9.8
##get the velocity and angle
##calculate it
##print time with some text


这方面的raw_input(),http:/ / / /图书馆/ 2 docs.python.org functions.html #原_输入: </P >

1
2
3
4
5
6
7
import math
v = raw_input("please enter the velocity:")
theta = raw_input("please enter the theta (i.e. degree of liftoff):")
v, theta = float(v) , float(theta)
t = (v * math.sin(theta)) / float(9.81)
print"assuming that g = 9.81"
print"projectile motion =", t

这方面的sys.argv,http:/ / / /图书馆/ 2 docs.python.org sys.html # sys.argv </P >

1
2
3
4
5
6
7
8
import sys, math
if len(sys.argv) == 3 and sys.argv[1].replace(".","").isdigit() and sys.argv[2].replace(".","").isdigit():
    v, theta = float(sys.argv[1]) , float(sys.argv[2])
    t = (v * math.sin(theta)) / float(9.81)
    print"assuming that g = 9.81"
    print"projectile motion =", t
else:
    print"Usage:","python %s velocity theta" % sys.argv[0]