Search code examples
pythonpython-3.xsyntaxloopback

python 3 loop back with if else elif


I'm trying to make a little game for my girlfriend to test my abilities with basic coding structures in python 3.10. I keep running into issues either with the program not running at all or getting infinite loopbacks using while True. another issue I have had is that when the else statement is triggered the computer simply moves on to the next line of code and won't loop back.

for this program specifically, it's a little "choose 1 2 or 3" program. I call it Cat Doors. you start off by choosing your cat 1 2 or 3 and they have names that display. if you enter a number or an input that is not 1 2 or 3 it spits out a statement telling you (well her my gf) to try again. I wanted it to loop back if the input was not 1 2 or 3 but I can't get it to cooperate. if the other statements triggered with 1 2 or 3 then it would ideally move on to the next line of code. another issue I was having was that the program closes after the last line is executed. I'm really new to this so please go easy on me haha.

number=input('CHOOSE! YOUR! CAT! 1 2 or 3')

if number == '1':
    print('you chose BIXBY! now choose your first door')
elif number == '2':
    print('you chose SASHA! now choose your first door.')
elif number == '3':
    print('you chose SHADOW! now choose your first door.')
else:
    print('you did not follow the instructions')    

number=input('you walk down a hallway. at the end of the hallway you come to 3 doors. you decide to go through door...')
   
if number == '1':
    print('door 1. you chase a cockroach down a low lit corridor.')
elif number == '2':
    print('door 2. you find a bed with a person in it. it is kimora! you make biscuits on the bean. then she kicks you out for being annoying. you are now in a low lit corridor')
elif number == '3':
    print('door 3. you find a ball of yarn. you paw and claw at the yarn until it rolls away. you follow the yarn to a low lit corridor')
else:
    print('you must use either 1 2 or 3 just type the number and call me if you need help')
    
number=input('you continue down the corridor until you come to another set of three doors. these doors have guilded knobs. shiny! kitty like door number...')

if number == '1':
    print('door 1. you walk through the door and hear birds. you are outside in a forest. you try to climb a tree to get the pretty bird. you lose your grip and fall into a cave cushioned by mushrooms!')
elif number == '2':
    print('door 2. you walk through the door and enter a lush forest. you are not a dog yet that squirrel over there really needs to die for no reason. you chase it into a cave with lots of mushrooms.')
elif number == '3':
    print('door 3. you run through the door and find yourself in a lush forest. there is a stream and you see yourself in it. there can obviously only be one of you so you decide to fight your reflection. you fall in the water and you are carried downstream to a cave with lots of mushrooms.')
else:
    print('you must use either 1 2 or 3 just type the number and call me if you need help')

id like the else statements to trigger a repeat of the line of code that theyre a part of. id also like the if and elifs to stigger the next statement (next to number=input) and then have it move to the next line of code


Solution

  • how does this work for you:

    number = input('CHOOSE! YOUR! CAT! 1 2 or 3')
    while number not in ['1', '2', '3']:
        print('you did not follow the instructions')
        number = input('CHOOSE! YOUR! CAT! 1 2 or 3')
    if number == '1':
        print('you chose BIXBY! now choose your first door')
    elif number == '2':
        print('you chose SASHA! now choose your first door.')
    elif number == '3':
        print('you chose SHADOW! now choose your first door.')
    

    there are sure fancier ways to do this, but this is straightforward and quite easy to understand