如何在Python中调试if和elif语句?

时间:2017-04-22 10:22:24

标签: python python-3.x

# Kinematics clculator

print('Kinematics Calculator')

print('If either one of the three values(s,v,t) is not given then put its value = 1')


s = float(input('Enter your distance =  '))
print(s)


t = float(input('Enter your time =  '))
print(t)


v = float(input('Enter your velocity =  '))
print(v)


if 'v == 1' :
    print('velocity = '+ str(s/t))

elif 't == 1' :
    print('time = '+ str(s/v))

else :
     's == 1' 
print('distance = '+ str(v*t)) 

帮我纠正这段代码。每当我尝试计算“速度”以外的任何东西时,它总是使用第一个打印命令,即

print('velocity = '+ str(s/t)) 

1 个答案:

答案 0 :(得分:4)

'v == 1'总是评估为true,因为它是非空字符串。你应该使用

if v == 1:
    print('velocity = '+ str(s/t))

elif t == 1:
    print('time = '+ str(s/v))

else:
     print('distance = '+ str(v*t))