Reputation: 1
I am trying to design a game where you wake up in a room and there are only 3 doors to get out. You start with 4 choices (1. left door, 1. Right door, 3. Middle door, 4. Stay where you are). This will then take you to either certain death or 3 more doors. This happens again till either you eventually find your way out after choosing correctly or death. I have designed the following code which runs it through the first 4 choices, however after this I run a blank. I was thinking do I need a while loop in there or am I going about it wrong from the start.
choice1 = """
1: door on the left,
2: door on the right,
3: door in the middle,
4: Stay where you are and hope to be rescued"""
choice2 = """
1: door on the left,
2: door on the right,
3: door in the middle,"""
door_left = """You have chosen the door on the left
are you sure? Y/N"""
door_right = """You have chosen the door on the right
are you sure? Y/N"""
door_middle = """You have chosen the door in the middle
are you sure? Y/N"""
axe = "A knight wielding a giant axe awaits you and chops off your head"
hole = """The room is pitch black and as you walk through you fall in to
a bottomless pit and spend the rest of your days falling in to darkness"""
crush = """The room in front of you is light and you believe
you have made the right choice, until you hear a noise and
when you look up you can see the ceiling in becoming lower.
This continues until it crushes you in to the floor"""
good_choice = f"""Well done you have chosen well, however you
are confronted by another 3 doors. Which door will you choose?
{choice2}"""
try_again = f"Ok choose another door {choice2}"
play = (input(f"""You have awoken in an empty low lit room with
no windows and only three doors. You have no
idea how you got here but you know you have to
choose one of the following
{choice1}\n"""))
if play == "1":
user_input = input(f"{door_left}")
if user_input.lower() == "y":
print({axe})
elif play == "2":
user_input = input(f"{door_middle}")
if user_input.lower() == "y":
print({hole})
elif play == "3":
user_input = input(f"{door_right}")
if user_input.lower() == "y":
user_input = print({good_choice})
elif play == "4":
print("""You wait it out hoping to be saved
1, 2, 3 days go by with none arriving
eventually after many days of no food
or water you become so dehydrated you
fall over into a deep sleep never to
awaken again.""")
else:
print("""You have failed too choose a correct
number, you hear all the doors lock and
a gas starts to appear from the walls. You
run over to the doors but they are all
locked, eventually the gas gets the better
of you and you drift off into a forever
slumber.""")
Upvotes: 0
Views: 93
Reputation: 432
As @hannabanana123 pointed out, you have to adjust the indentation properly. You also have to add the loop with a termination condition. I have assumed that, once you die the game ends. Finally, if you want to change the question asked at each iteration once you get past the first door, you have to create a variable for it.
All in all, I have put together a first approximation to your code as follows:
choice1 = """
1: door on the left,
2: door in the middle,
3: door on the right,
4: Stay where you are and hope to be rescued"""
choice2 = """
1: door on the left,
2: door on the middle,
3: door in the right,"""
door_left = """You have chosen the door on the left
are you sure? Y/N"""
door_right = """You have chosen the door on the right
are you sure? Y/N"""
door_middle = """You have chosen the door in the middle
are you sure? Y/N"""
axe = "A knight wielding a giant axe awaits you and chops off your head"
hole = """The room is pitch black and as you walk through you fall in to
a bottomless pit and spend the rest of your days falling in to darkness"""
crush = """The room in front of you is light and you believe
you have made the right choice, until you hear a noise and
when you look up you can see the ceiling in becoming lower.
This continues until it crushes you in to the floor"""
good_choice = f"""Well done you have chosen well, however you
are confronted by another 3 doors. Which door will you choose?
{choice2}"""
try_again = f"Ok choose another door {choice2}"
done = False
question = f"""You have awoken in an empty low lit room with
no windows and only three doors. You have no
idea how you got here but you know you have to
choose one of the following
{choice1}\n"""
while not done:
play = (input(f'{question}'))
if play == "1":
user_input = input(f"{door_left}")
if user_input.lower() == "y":
print(f'{axe}')
done = True
elif play == "2":
user_input = input(f"{door_middle}")
if user_input.lower() == "y":
print(f'{hole}')
done = True
elif play == "3":
user_input = input(f"{door_right}")
if user_input.lower() == "y":
question = f'{good_choice}'
elif play == "4":
print("""You wait it out hoping to be saved
1, 2, 3 days go by with none arriving
eventually after many days of no food
or water you become so dehydrated you
fall over into a deep sleep never to
awaken again.""")
done = True
else:
print("""You have failed too choose a correct
number, you hear all the doors lock and
a gas starts to appear from the walls. You
run over to the doors but they are all
locked, eventually the gas gets the better
of you and you drift off into a forever
slumber.""")
done = True
However, you can see that you have a lot of repetition in your code, so you could introduce a function for the "deaths"
def play_bad_choice(input_text, death_text):
user_input = input(f"{input_text}")
if user_input.lower() == "y":
print(f'{death_text}')
return True
return False
This would cover options 1 and 2, where you ask for a confirmation and you die if you say "yes" or survive otherwise. You can also extend the function to cover the non-confirmed option 4 as follows:
def play_bad_choice(input_text, death_text):
if input_text is not None:
confirm_bad_choice = input(f"{input_text}")
if input_text is None or confirm_bad_choice.lower() == "y":
print(f'{death_text}')
return True
return False
Putting everything together it would look something like:
choice1 = """
1: door on the left,
2: door in the middle,
3: door on the right,
4: Stay where you are and hope to be rescued"""
choice2 = """
1: door on the left,
2: door on the middle,
3: door in the right,"""
door_left = """You have chosen the door on the left
are you sure? Y/N"""
door_right = """You have chosen the door on the right
are you sure? Y/N"""
door_middle = """You have chosen the door in the middle
are you sure? Y/N"""
axe = "A knight wielding a giant axe awaits you and chops off your head"
hole = """The room is pitch black and as you walk through you fall in to
a bottomless pit and spend the rest of your days falling in to darkness"""
crush = """The room in front of you is light and you believe
you have made the right choice, until you hear a noise and
when you look up you can see the ceiling in becoming lower.
This continues until it crushes you in to the floor"""
good_choice = f"""Well done you have chosen well, however you
are confronted by another 3 doors. Which door will you choose?
{choice2}"""
try_again = f"Ok choose another door {choice2}"
done = False
question = f"""You have awoken in an empty low lit room with
no windows and only three doors. You have no
idea how you got here but you know you have to
choose one of the following
{choice1}\n"""
def play_bad_choice(input_text, death_text):
if input_text is not None:
confirm_bad_choice = input(f"{input_text}")
if input_text is None or confirm_bad_choice.lower() == "y":
print(f'{death_text}')
return True
return False
while not done:
play = (input(f'{question}'))
if play == "1":
done = play_bad_choice(door_left, axe)
elif play == "2":
done = play_bad_choice(door_middle, hole)
elif play == "3":
user_input = input(f"{door_right}")
if user_input.lower() == "y":
question = f'{good_choice}'
elif play == "4":
done = play_bad_choice(None, """You wait it out hoping to be saved
1, 2, 3 days go by with none arriving
eventually after many days of no food
or water you become so dehydrated you
fall over into a deep sleep never to
awaken again.""")
else:
done = play_bad_choice(None, """You have failed too choose a correct
number, you hear all the doors lock and
a gas starts to appear from the walls. You
run over to the doors but they are all
locked, eventually the gas gets the better
of you and you drift off into a forever
slumber.""")
I hope it helps
Upvotes: 0