-1

I am new to Python and taking a class for it. I am making a 'choose your own story' game where users put either 'y' or 'n' to lead to another situation. Here is part of what I have:

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")
AKX
  • 152,115
  • 15
  • 115
  • 172
  • 1
    Would [this question](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) be what you mean to ask? – Thierry Lathuille Mar 23 '21 at 21:05

2 Answers2

2

For one, you'll want only one input() call, and you'll need to store the response in a variable.

Then, you'll need a loop in case to repeat the prompt in case they give an incorrect reply:

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")

Then, foreseeing there will be many more of this kind of prompt in your game, we can wrap this in a function that returns True or False. (I took the opportunity of also adding lower-casing, so either Y or y (or N/n) work.)

def prompt_yes_no(question):
    while True:
        response = input(question).lower()
        if response == "y":
            return True
        elif response == "n":
            return False
        print("Please put y or n")


if prompt_yes_no("Do you want to open the door?"):
    print("You opened the door. It is a mostly empty room but you spot something in the corner")
else:
    print("You decided not to enter the room, you move down the hallway")
AKX
  • 152,115
  • 15
  • 115
  • 172
0

To compare your input with "y" and "n", you should set a variable to store the input.

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")
Austin He
  • 131
  • 3