Python计算器运动学

2024-10-01 00:31:05 发布

您现在位置:Python中文网/ 问答频道 /正文

嘿,伙计们,我要做的是做一个非常基本的运动学变量解算器,所以我得到了基本方程vf=vi+at。现在我要做的是,如果你不知道这个变量,你输入/哪个是你要解的变量。但问题是,当我尝试输入变量as/时,它会给我一个错误,因为我使用的是整数。但是如果我从每个变量中去掉int(),它就不会让方程起作用!我被卡住了,如果有人能给我一些建议,我将不胜感激。好像我的图片没有上传,所以这里有一个gyazo链接https://gyazo.com/3325b42c51f839dc901cdefc1fe9b7fd

vf = input("What is the final velocity?")
if vf == "/":
 dontuse = "vf"
else:
    pass
vi = int(input("What is the intial velocity?"))
if vi == "/":
  dontuse = "vi"
else:
  pass
a = int(input("What is the acceleration?"))
if a == "/":
  dontuse = "a"
else:
  pass
t = int(input("What is the time?"))
if t == "/":
  dontuse = "t"
else:
  pass
def eq1():
 vf = vi + a*t
def eq2():
  vi = vf/(a*t)
def eq3():
  t = (vf - vi)/a
if dontuse == "vf":
  eq1()

Tags: theinputifisdefpasswhatelse
1条回答
网友
1楼 · 发布于 2024-10-01 00:31:05

好的,问题是你以Int的形式输入,但是你要求用户输入“/”,这是一个字符串。 在某些版本中,这不会给出错误,但在这里会给出错误

第二,你在计算Int变量,这是无效的乘法和除法,所以我用浮点,这是更好的准确性。你知道吗

dontuse=''

vf = input("What is the final velocity?")
if vf == "/":
 dontuse = "vf"
else:
    vf=float(vf)

vi = str(input("What is the floatial velocity?"))
if vi == "/":
  dontuse = "vi"
else:
  vi=float(vi)

a = str(input("What is the acceleration?"))
if a == "/":
  dontuse = "a"
else:
  a=float(a)

t = str(input("What is the time?"))
if t == "/":
  dontuse = "t"
else:
  t=float(t)


def eq1():
    vf = vi + a*t
    print("vf: "+str(vf))
def eq2():
    vi = vf/(a*t)
    print("vi: "+str(vi))
def eq3():
    t = (vf - vi)/a
    print("t: "+str(t))

def eq4():
    a = (vf - vi)/t
    print("a: "+str(a))



if dontuse == "vf":
    eq1()
elif dontuse=='vi':
    eq2()
elif dontuse=='a':
    eq4()
elif dontuse=='t':
    eq3()
else:
    print('All value known')

享受:)

相关问题 更多 >