首页 > 解决方案 > != 符号被忽略?

问题描述

我知道我很愚蠢,但我不知道怎么做。对于这个程序,用户必须输入 1 或 2,1 将英寸转换为 CM,2 将 CM 转换为英寸。

现在的问题是,如果他们输入的数字不等于 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)

标签: pythonpython-3.x

解决方案


if ask_user != 1 and ask_user != 2:
    print("Sorry, please enter either 1 or 2!")
    return 

推荐阅读