python 3使用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 旁边),然后让它移动到下一行代码
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这对你来说如何工作:
肯定有更奇特的方法可以做到这一点,但这很简单并且很容易理解
how does this work for you:
there are sure fancier ways to do this, but this is straightforward and quite easy to understand
关键思想是使用 while True 循环来获取 input() 直到输入预期的输入 [1,2,3]。
PS:我已经把你的选项变成了数字,但这只是个人喜好。
PS2:快乐编码!
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!