0

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

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

5 Comments

I tried this one and it didnt work the program kept crashing on me when I tried to open it after saving. Maybe i did something wrong? I wish there was like a code checker that could tell me why it doesnt work
Post your whole program and the error message when it crashes
It doesnt really crash persae it like just will not start rather like I double click and nothing happens or it opens and closes really fast
Oh I figured it out I forgot a colon at the end of the while True statement!
Well done figuring it out :)
0

The key idea is to use while True loop to get input() until expected input [1,2,3] is input. PS: I have turned your option into digits, but it is just personal preference. PS2: happy coding!

def step_1(number):
    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.')
    generic_call_input('you walk down a hallway. at the end of the hallway you come to 3 doors. you decide to go through door...',step_2)


def step_2(number):
    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')
    generic_call_input("step 3 msg",step_3)

def step_3(number):
    if number == 1:
        print('1')
    elif number == 2:
        print('2')
    elif number == 3:
        print('3')
    

def generic_call_input(input_msg,callback):

    while True:
        number = input(input_msg)
        try:
            number = int(number)
        except:
            pass
        if number in [1,2,3]:
            callback(number)
            break


generic_call_input("CHOOSE! YOUR! CAT! 1 2 or 3",step_1)


5 Comments

What is the message call back thing? Again I'm brand newish I only know about some conditionals
In coding there is a DRY principle (en.wikipedia.org/wiki/Don%27t_repeat_yourself) . A generic function is required to abstract the input process with 2 elements. 1. what message to display when asking user for input (input_msg variable). 2. what function (callback) to be used when the correct input is received.
Oh cool that makes sense thanks for explaining it
Sorry for all the questions but where would the instructions for the first step of choosing the cat go? Would the go where step_1 goes?
And additionally what happens if a number outside of the range is entered like 16 or 7 or something? There is no else to catch that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.