简体   繁体   English

python 3 循环返回 if else elif

[英]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.我正在尝试为我的女朋友制作一个小游戏来测试我在 python 3.10 中使用基本编码结构的能力。 I keep running into issues either with the program not running at all or getting infinite loopbacks using while True.我一直遇到程序根本不运行或使用 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.我遇到的另一个问题是,当 else 语句被触发时,计算机只是简单地移动到下一行代码并且不会循环返回。

for this program specifically, it's a little "choose 1 2 or 3" program.特别是对于这个程序,它是一个“选择 1 2 或 3”的小程序。 I call it Cat Doors.我称之为猫门。 you start off by choosing your cat 1 2 or 3 and they have names that display.您首先选择您的猫 1 2 或 3,它们会显示名称。 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.如果您输入一个数字或不是 1 2 或 3 的输入,它会吐出一条语句告诉您(她是我的女朋友)再试一次。 I wanted it to loop back if the input was not 1 2 or 3 but I can't get it to cooperate.如果输入不是 1 2 或 3,我希望它循环返回,但我无法让它合作。 if the other statements triggered with 1 2 or 3 then it would ideally move on to the next line of code.如果其他语句以 1 2 或 3 触发,那么理想情况下它会转到下一行代码。 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.我对此很陌生,所以请 go 对我放轻松哈哈。

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 喜欢 else 语句来触发它们所属的代码行的重复。 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 id 也喜欢 if 和 elifs 来触发下一条语句(在 number=input 旁边)然后让它移动到下一行代码

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肯定有更奇特的方法可以做到这一点,但这很简单,也很容易理解

The key idea is to use while True loop to get input() until expected input [1,2,3] is input.关键思想是使用while True循环获取 input() 直到输入预期的输入 [1,2,3]。 PS: I have turned your option into digits, but it is just personal preference. PS:我把你的选项变成了数字,但这只是个人喜好。 PS2: happy coding! PS2:快乐编码!

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)


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM