无法弄清楚为什么我的 else 语句仍然使用负输入调用此函数



我正在自学Python,并决定制作一个简短的基于文本的游戏,首先我决定为用户制作一个函数,以确认他们确实想要玩游戏并使用"是"或"否";选项,然而,即使给出了"不";输入时,游戏函数仍然被调用。如有任何帮助,我将不胜感激。

这是进入游戏的命令提示符:

while i <= 10000000000000000:
command = input(":")
if command == "List commands":
command_list()
i += 1
elif command == "Atlas":
atlas_confirmation()
i += 1

这是确认用户想要玩游戏的提示符:

def atlas_confirmation():
print("Alright, but this one's pretty spooky. Are you sure?")
yn = input(":")
if yn == "Yes" or "yes" or "I'm sure" or "Im sure" or "im sure" or "i'm sure":
atlas_game()
elif yn == "No" or "no":
print(command_end)

这是游戏的占位符:

def atlas_game():
print(placeholder)
print(command_end)

当给定一个正输入时,我得到期望的输出:

Uh oh! Looks like this code has yet to be completed, but it will be available soon!
Is there anything else I can do for you?
:

然而,当我给出一个负输入时,输出仍然与上面的^相同,就好像我给出了一个正输入,而不是期望的输出:

Is there anything else I can do for you?
:

我希望我的'if'语句是错误的地方,但我不知道在哪里或为什么。

任何帮助都将不胜感激,

谢谢。

我认为您需要从atlas_confirmation()函数返回command_end以检查while循环中的中断条件。更新你的atlas_confirmation()函数如下:

def atlas_confirmation():
print("Alright, but this one's pretty spooky. Are you sure?")
yn = input(":")
if yn == "Yes" or yn == "yes" or yn == "I'm sure" or yn == "Im sure" or yn == "im sure" or yn == "i'm sure":
atlas_game()
elif yn == "No" or yn == "no":
return command_end

和你的while循环到这个:

while i <= 10000000000000000:
command = input(":")
if command == "List commands":
command_list()
i += 1
elif command == "Atlas":
atlas_confirmation()
i += 1

最新更新