Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python != 标志被忽略了?_Python_Python 3.x - Fatal编程技术网

Python != 标志被忽略了?

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

我知道我很愚蠢,但我不知道怎么做。对于此程序,用户必须输入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 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