我是 Python 新手,我正在尝试让下面的代码运行,但是 else 语句不起作用:
print("What's your name?")
my_name = input()
if my_name == "Simon" or "simon":
print("Hello, Simon!")
else:
print("Hello, stranger!")
答案1
您需要对每个字符串进行单独的比较,然后or
像这样连接它们
if my_name == "Simon" or my_name == "simon":
完整代码
print("What's your name?")
my_name = input()
if my_name == "Simon" or my_name == "simon":
print("Hello, Simon!")
else:
print("Hello, stranger!")
示例输出
wolf@linux:~$ py script.py
What's your name?
hey
hey
Hello, stranger!
wolf@linux:~$
wolf@linux:~$
wolf@linux:~$ py script.py
What's your name?
simon
simon
Hello, Simon!
wolf@linux:~$
希望这可以帮助。