python 3 循环回用 if else elif
python 3 loop back with if else elif
我正在尝试为我的女朋友制作一个小游戏来测试我在 python 3.10 中使用基本编码结构的能力。我一直 运行 遇到问题,要么是程序根本不是 运行,要么是使用 while True 获得无限循环。我遇到的另一个问题是,当 else 语句被触发时,计算机只是移动到下一行代码并且不会循环返回。
具体来说,这是一个“选择 1 2 或 3”的小程序。我称之为猫门。您首先选择您的猫 1 2 或 3,它们会显示名称。如果您输入一个数字或不是 1 2 或 3 的输入,它会吐出一条语句告诉您(她是我的女朋友)再试一次。如果输入不是 1 2 或 3,我希望它循环返回,但我无法让它合作。如果其他语句以 1 2 或 3 触发,那么理想情况下它会转到下一行代码。我遇到的另一个问题是程序在最后一行执行后关闭。我真的很新,所以请放轻松哈哈。
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 像 else 语句一样触发重复它们所属的代码行。 id 也喜欢 if 和 elifs 来启动下一个语句(在 number=input 旁边)然后让它移动到下一行代码
这对你有何作用:
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.')
肯定有更奇特的方法可以做到这一点,但这很简单也很容易理解
关键思想是使用while True
循环获取input(),直到输入预期的输入[1,2,3]。
PS: 我把你的选项变成了数字,但这只是个人喜好。
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)
我正在尝试为我的女朋友制作一个小游戏来测试我在 python 3.10 中使用基本编码结构的能力。我一直 运行 遇到问题,要么是程序根本不是 运行,要么是使用 while True 获得无限循环。我遇到的另一个问题是,当 else 语句被触发时,计算机只是移动到下一行代码并且不会循环返回。
具体来说,这是一个“选择 1 2 或 3”的小程序。我称之为猫门。您首先选择您的猫 1 2 或 3,它们会显示名称。如果您输入一个数字或不是 1 2 或 3 的输入,它会吐出一条语句告诉您(她是我的女朋友)再试一次。如果输入不是 1 2 或 3,我希望它循环返回,但我无法让它合作。如果其他语句以 1 2 或 3 触发,那么理想情况下它会转到下一行代码。我遇到的另一个问题是程序在最后一行执行后关闭。我真的很新,所以请放轻松哈哈。
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 像 else 语句一样触发重复它们所属的代码行。 id 也喜欢 if 和 elifs 来启动下一个语句(在 number=input 旁边)然后让它移动到下一行代码
这对你有何作用:
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.')
肯定有更奇特的方法可以做到这一点,但这很简单也很容易理解
关键思想是使用while True
循环获取input(),直到输入预期的输入[1,2,3]。
PS: 我把你的选项变成了数字,但这只是个人喜好。
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)