0

After some looking around, this is most likely what I'm looking for, but I can't understand it at all, nor can I figure out how to apply it to my code.

Python Input Prompt Go Back

So basically I'm trying to make a basic text based game and I have multiple doors which will say certain things/drop hints for something to come, so I want to give the user a chance to open all of the doors.

Right now if they choose the wrong door they're forced to go back, which is how I want it, but if they choose the right door they're forced to go forward, I'd like to add a "go back" option but I can't figure out how... I'm extremely new to Python so if someone could help me out in extremely simple terms I would really appreciate it, thanks guys, here's my code.

#Def#
def purple_door():'''I'd like it to have the option to type "continue" or "go back"'''
print('"You open the door to find a long dark hall, at the end you can see a glowing white light, perhaps a shiny medal! Oooo shiny medal!')
input('continue')

def red_door():
    print('This door opens1')

def orange_door():
    print('This door opens2')

def yellow_door():
    print('This door opens3')

def green_door():
    print('This door opens4')

def blue_door():
    print('This door opens5')

def pink_door():
    print('This door opens6')

#End Def#

#If or Else#

while True:

    door_chosen = input('>')

    if door_chosen in ('Purple', 'purple', 'Purple.', 'purple.'):
        purple_door()
        break

    elif door_chosen in ('Red', 'red', 'Red.', 'red.'):
        red_door()

    elif door_chosen in ('Orange', 'orange', 'Orange.', 'orange.'):
        orange_door()

    elif door_chosen in('Yellow', 'yellow', 'Yellow.', 'yellow.'):
        yellow_door()

    elif door_chosen in('Green', 'green', 'Green.', 'green.'):
        green_door()

    elif door_chosen in ('Blue', 'blue', 'Blue.', 'blue.'):
        blue_door()

    elif door_chosen in ('Pink', 'pink', 'Pink.', 'pink.'):
        pink_door()

    else:
        print('Please type a color stated above.')
#If or else end#

Any suggestions for improvement are greatly appreciated.

Edit: I guess I didn't explain well enough my apologies.

Yes when any door other than Purple is chosen, it forces you to choose another until you choose purple, this is what I want to happen.

But let's say someone wants to open all the doors and their first guess was purple, now they have to move on, so what I would like to do is give the option to go back, say. ///Example/// "Choose which door you'd like to open"

Purple

"You open the purple door and come to a hall" "Continue, or go back"

Go back

"You decide to go back and look at other doors" ///End///

Then you can choose purple and continue when you're ready. I hope that makes more sense now.

Akuma-Yagi
  • 13
  • 4
  • 3
    Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Chris_Rands Sep 27 '17 at 14:35

2 Answers2

0

The reason your program doesn't allow the user to go back is because you're probably only testing the purple door which contains the break statement. Every other door will ask for the user to choose another door:

Also note my changes using .lower() to reduce the amount of values you need to check against

def purple_door():
    print('"You open the door to find a long dark hall, at the end you can see a glowing white light, perhaps a shiny medal! Oooo shiny medal!')

def red_door():
    print('This door opens1')

while True:
    door_chosen = input('Choose a door colour: ')

    if door_chosen.lower() in ('purple', 'purple.'):
        purple_door()
    elif door_chosen.lower() in ('red', 'red.'):
        red_door()
    else:
        print('Please type a color stated above.')

Example output:

Choose a door colour: red
This door opens1
Choose a door colour: purple
"You open the door to find a long dark hall, at the end you can see a glowing white light, perhaps a shiny medal! Oooo shiny medal!"
Choose a door colour: black
Please type a color stated above.
Choose a door colour: red.
This opens door1
Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65
0

What you can do is make the #If Else# section of your code into a function. From there, you call that function at the end of purple_door if some input there == go back. I made some other readability improvements along the way:

color_num = {'red': 1, 'orange': 2, 'yellow': 3, 'green': 4, 'blue': 5, 'pink': 6}  # for later use


def purple_door():
    print('"You open the door to find a long dark hall, at the end you can see a glowing white light, perhaps a shiny medal! Oooo shiny medal!"')
    while True:
        choice = input('Continue? ').lower().strip(' .')

        if choice in {'continue', 'yes', 'ok', 'why not', 'may as well'}:
            # Continue the game
            break

        elif choice in {'no', 'back', 'go back', 'do not continue', 'retreat'}:
            main()
            break

        else:
            continue

def main():
    while True:
        door_chosen = input('>').lower().strip(' .')  # lower forces the string to lowercase
                                                      # and strip(' .') removes spaces and periods from the beginning and end
        if door_chosen == 'purple':
            purple_door()
            break

        elif door_chosen in color_num.keys():
            print('This door opens{}'.format(color_num[door_chosen]))  # inserts the number corresponding to the door in place of the {}

        else:
            print('Please type a color stated above.')

main()
Eric Ed Lohmar
  • 1,832
  • 1
  • 17
  • 26
  • Thanks! That worked out pretty much how I wanted, one last question, say I want an individual description for each door opened, how do I do that? – Akuma-Yagi Sep 27 '17 at 19:49
  • Instead of putting number values in the dictionary, set them to the strings you want to print. Then, instead of `print('This door opens{}'.format(color_num[door_chosen]))` just put `print(color_num[door_chosen])` – Eric Ed Lohmar Sep 27 '17 at 20:08
  • Perfect! Case closed, it's working how I expected it to! Thank you very much. – Akuma-Yagi Sep 27 '17 at 20:29