Python 如果没有';y';或';n';

Python 如果没有';y';或';n';,python,python-3.8,Python,Python 3.8,我对Python还不熟悉,并为此上了一堂课。 我正在制作一个“选择你自己的故事”游戏,用户在游戏中输入“y”或“n”以导致另一种情况。 以下是我的一部分: print("Do you want to open the door") if input() == "y": print("You opened the door. It is a mostly empty room but you spot something in the c

我对Python还不熟悉,并为此上了一堂课。 我正在制作一个“选择你自己的故事”游戏,用户在游戏中输入“y”或“n”以导致另一种情况。 以下是我的一部分:

print("Do you want to open the door")

if input() == "y":
    print("You opened the door. It is a mostly empty room but you spot something in the corner")

elif input() == "n":
    print("You decided not to enter the room, you move down the hallway")

if input() != "y" or "n":
    print("Please put y or n")

首先,您只需要一个
input()
调用,并且需要将响应存储在变量中

然后,您需要一个循环,以便在他们给出错误答复时重复提示:

while True:
    response = input("Do you want to open the door?")

    if response == "y":
        print("You opened the door. It is a mostly empty room but you spot something in the corner")
        break
    elif response == "n":
        print("You decided not to enter the room, you move down the hallway")
        break
    print("Please put y or n")
然后,预见到在你的游戏中会有更多这样的提示,我们可以将其封装在一个返回True或False的函数中。(我也借此机会添加了下套管,以便
Y
Y
(或
N
/
N
)工作。)


要将输入与“y”和“n”进行比较,应设置一个变量来存储输入

print("Do you want to open the door")
x=input()
if x == "y":
    print("You opened the door. It is a mostly empty room but you spot something in the corner")

elif x == "n":
    print("You decided not to enter the room, you move down the hallway")

else:
    print("Please put y or n")
你的意思是什么?
print("Do you want to open the door")
x=input()
if x == "y":
    print("You opened the door. It is a mostly empty room but you spot something in the corner")

elif x == "n":
    print("You decided not to enter the room, you move down the hallway")

else:
    print("Please put y or n")