[英]Strange error in python3
我做了一个简单的管理程序,但看起来有一个奇怪的错误。 这是我的代码:
scelta = 0
print("Welcome in the program 'Be in time at home'")
bus = input("Which bus do you need (From university you can choose between bus 5 or 6)? ")
if bus != "5" or bus != "6":
print("The bus you choose does not go through Universita'")
program()
else:
if bus == "5":
print("You can choose between 2 directions: Viganello and Lamone Cadempino")
way = input("Make your choice for the direction: ")
else:
print("You can choose between 2 directions: Lugano Stazione and Cornaredo")
way = input("Make your choice for the direction: ")
问题是:即使我按5
或6
,程序也会在错误的if语句中运行( "The bus you choose does not go..."
)。 这怎么可能? 看起来很简单。
问题在于您在此行上的if语句条件:
if bus != "5" or bus != "6":
bus
将始终不等于"5"
或不等于"6"
。 您应该使用and
:
if bus != "5" and bus != "6":
那或者你可以not in
使用
if bus not in {"5", "6"}:
如果您要测试许多值,这将特别有用。
如果语句始终为True
:
if bus != "5" or bus != "6"
此时应更换or
与and
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.