2014-02-01 2 views
-4

Я только начинаю питон.Как получить ввод от пользователя и применить вычисления в 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 
+0

См. Http://stackoverflow.com/questions/70797/python-and-user-input – axiom

+0

Обязательно посмотрите на верхние * два * ответы из ссылки @ axiom. Они совершенно разные подходы; один из них, вероятно, то, что вы хотите – mhlester

+0

raw_input() или input() является другом. –

ответ

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] 
Смежные вопросы