Python != 标志被忽略了?
我知道我很愚蠢,但我不知道怎么做。对于此程序,用户必须输入1或2,1将英寸转换为厘米,2将厘米转换为英寸 现在的问题是,如果他们输入的数字不等于1或2,它应该显示错误“对不起,请输入1或2”。尽管当用户未输入1或2时会显示错误消息,但当我输入正确的输入1或2时,它仍会显示该错误 有什么解决办法吗?谢谢Python != 标志被忽略了?,python,python-3.x,Python,Python 3.x,我知道我很愚蠢,但我不知道怎么做。对于此程序,用户必须输入1或2,1将英寸转换为厘米,2将厘米转换为英寸 现在的问题是,如果他们输入的数字不等于1或2,它应该显示错误“对不起,请输入1或2”。尽管当用户未输入1或2时会显示错误消息,但当我输入正确的输入1或2时,它仍会显示该错误 有什么解决办法吗?谢谢 ask_user = int(input("Enter 1 to convert inches to centimetres or 2 to convert centimetres to inch
ask_user = int(input("Enter 1 to convert inches to centimetres or 2 to convert centimetres to inches: "))
#This is the error below, it prints this statement when you enter 1 or 2**
if ask_user != 1 or 2:
print("Sorry, please enter either 1 or 2!")
if ask_user == 1:
inch = int(input("Enter the amount of Inches you wish to convert to Centimeter: "))
cm = inch * 2.54
print(cm)
if ask_user == 2:
cm = int(input("Enter the amount of Centimeter you wish to convert to Inch(s): "))
inch = cm * 0.393701
print(inch)
如果询问用户不在(1,2):
这是错误的!看:ask_user=1
然后:1!=1
?FALSE,则:1!=3
?对。所以
if ask_user != 1 and ask_user != 2:
print("Sorry, please enter either 1 or 2!")
return