python3中的奇怪错误

时间:2014-11-29 18:38:23

标签: python if-statement

我做了一个简单的管理程序,但看起来我有一个奇怪的错误。这是我的代码:

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: ")

问题是:即使我按56,程序也会使用错误的if语句("The bus you choose does not go...")。这怎么可能?看起来很容易。

2 个答案:

答案 0 :(得分:3)

问题在于此行上的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"}:

如果您要测试多个值,这将变得特别有用。

答案 1 :(得分:0)

if语句始终为True

if bus != "5" or bus != "6"

您应该将or替换为and