python if else语句具有多个变量的问题

时间:2020-10-03 03:45:47

标签: python if-statement

在下面的代码中寻求帮助,似乎在第一个if语句上添加m_Time会使程序无法正确运行。仅查看第一个if语句,其余的按预期工作。任何对此的帮助将不胜感激。

tick_Num = float(input("How many tickets are you buying? "))
day = input("What day do you want to watch the movie? Enter mon for Monday etc. ")
m_Time = int(input("What time is the movie? Enter 0000 to 2359: "))


if 200 <= m_Time <= 1600 and tick_Type == "child" and day != "sat" or day != "sun":
    print("You should be in school, not at the movies.")
elif tick_Type == "child" and day == "mon" or day == "tue" or day == "wed":
    total = 8 * tick_Num * 0.75
    print("Total: $" + format(total,",.2f"))
elif tick_Type == "senior" and day == "mon" or day == "tue" or day == "wed":
    total = 9 * tick_Num * 0.75
    print("Total: $" + format(total,",.2f"))
elif tick_Type == "general" and day == "mon" or day == "tue" or day == "wed":
    total = 10 * tick_Num * 0.75
    print("Total: $" + format(total,",.2f"))
elif tick_Type == "child" and day == "thu" or day == "fri" or day == "sat":
    total = 8 * tick_Num * 1
    print("Total: $" + format(total,",.2f"))
elif tick_Type == "senior"  and day == "thu" or day == "fri" or day == "sat":
    total = 9 * tick_Num * 1
    print("Total: $" + format(total,",.2f"))
elif tick_Type == "general" and day == "thu" or day == "fri" or day == "sat":
    total = 10 * tick_Num * 1
    print("Total: $" + format(total,",.2f"))
elif tick_Type == "child"  and day == "sun":
    total = 8 * tick_Num * 0.9
    print("Total: $" + format(total,",.2f"))
elif tick_Type == "senior" and day == "sun":
    total = 9 * tick_Num * 0.9
    print("Total: $" + format(total,",.2f"))
elif tick_Type == "general" and day == "sun":
    total = 10 * tick_Num * 0.9
    print("Total: $" + format(total,",.2f"))
elif day == "mon" or day =="tus" or day == "wed":
    total = 10 * tick_Num * 0.75
    print("Total: $" + format(total,",.2f"))
elif day == "thu" or day =="fri" or day == "sat":
    total = 10 * tick_Num * 1
    print("Total: $" + format(total,",.2f"))
elif day == "sun":
    total = 10 * tick_Num * 0.9
    print("Total: $" + format(total,",.2f"))

else:
    print("NO TICKET: invalid day")

1 个答案:

答案 0 :(得分:1)

假设我正确地识别了您的问题,则由于Python计算布尔值的方式,如果200 <= m_Time <= 1600 and tick_Type == "child" and day != "sat" or day != "sun"为True,则第一个子句day != 'sun'始终为True。我相信您想要的是这样的:

if 200 <= m_Time <= 1600 and tick_Type == "child" and day != "sat" and day != "sun":
...