Python:即使if语句等于false,它仍然执行if语句中的代码

时间:2016-07-21 01:17:38

标签: python if-statement

即使if语句等于false,它仍然执行if语句中的代码。 这是代码:

# Imports:
import time

# Code Starts Here:
print("Welcome to a test application By: David(Toxicminibro)")
time.sleep(1.25)

Donald = "Donald Trump"
donald = "donald trump"
Hillary = "Hillary Clinton"
hillary = "hillary clinton"

name = input("What is your name?: ")

if name == Donald or donald or Hillary or hillary:
    print("No. Stop it.")
else:
    print("Hello " + name + " !")

3 个答案:

答案 0 :(得分:2)

我认为你的意思是:

if name == Donald or name == donald or name == Hillary or name == hillary:

拥有look at this link;它解释了各种价值如何被认为是真实的"或" false"

答案 1 :(得分:2)

if name in (Donald, donald, Hillary, hillary):
    print("No. Stop it.")
else:
    print("Hello " + name + " !")

答案 2 :(得分:1)

更多Pythonic的做法是:if name.lower() in [donald, hillary]: