【问题标题】:How to get input from user and apply calculation in Python? [duplicate]如何从用户那里获取输入并在 Python 中应用计算? [复制]
【发布时间】:2014-02-25 01:01:11
【问题描述】:

我刚刚开始python。

我想从用户那里获取输入并计算它。

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

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

【问题讨论】:

标签: python


【解决方案1】:

使用raw_input(),见http://docs.python.org/2/library/functions.html#raw_input

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://docs.python.org/2/library/sys.html#sys.argv

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]

【讨论】:

    猜你喜欢
    • 2021-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    • 2020-03-26
    • 2014-12-28
    相关资源
    最近更新 更多