命令后返回菜单? Python
我知道我所有的问题都很简单,但我是初学者,所以这里是...... 在大家的帮助下我一直在开发猜数字的东西,但我想然后返回到刚刚留下的菜单。这是代码:
import time
import random
animalmenu()
def animalmenu():
print()
print()
print()
print()
print('Welcome to the menu. I am thinking of a menu. Select the option\'s below to try and guess my animal.')
print()
print('a) No. of Legs')
print('b) Type of animal')
print('c) Preffered Climate')
print('d) Size')
print('e) Colour')
print('f) Diet')
print('g) Habitat')
print('h) Can be kept as pet')
print('i) Guess animal')
print()
print('When in a menu, type in \'555\' to return here')
AniChoice = input('Choose your option: ')
if AniChoice == 'a':
loop = 10
while loop == 10:
print()
print('')
print()
guessleg = int(input('Guess the number of legs: '))
if leg == guessleg:
print('True')
elif leg != guessleg:
print('False')
print('r = Return to menu, g = guess again.')
rg = input()
if rg == 'g':
print('Loading...')
elif rg == 'r':
loop = 0
time.sleep(1)
print('Returning to menu...')
time.sleep(1)
animalmenu()
每次运行它时,我都会按照代码的要求输入一个数字,但随后,它不会询问我是否要返回菜单,而是会一次又一次地询问这个问题,“猜猜腿的数量:”。我知道这与我的循环方法有关,但我不明白,并且由于整数设置,我不能再创建另一个 if,如下所示:
guessleg = int(input('Guess the number of legs: '))
if leg == guessleg:
print('True')
elif leg != guessleg:
print('False')
elif guessleg == 'back':
loop = 0
animalmenu()
而且我没有看到任何其他方法可以做到这一点,因为这两种方法似乎都不起作用?您建议如何返回animalmenu()?
I know all my questions are really easy but I'm a beginner so here it is...
I have been developing the guessing number thing after everyones help, but I want to then return to a menu which has just been left. Here's the code:
import time
import random
animalmenu()
def animalmenu():
print()
print()
print()
print()
print('Welcome to the menu. I am thinking of a menu. Select the option\'s below to try and guess my animal.')
print()
print('a) No. of Legs')
print('b) Type of animal')
print('c) Preffered Climate')
print('d) Size')
print('e) Colour')
print('f) Diet')
print('g) Habitat')
print('h) Can be kept as pet')
print('i) Guess animal')
print()
print('When in a menu, type in \'555\' to return here')
AniChoice = input('Choose your option: ')
if AniChoice == 'a':
loop = 10
while loop == 10:
print()
print('')
print()
guessleg = int(input('Guess the number of legs: '))
if leg == guessleg:
print('True')
elif leg != guessleg:
print('False')
print('r = Return to menu, g = guess again.')
rg = input()
if rg == 'g':
print('Loading...')
elif rg == 'r':
loop = 0
time.sleep(1)
print('Returning to menu...')
time.sleep(1)
animalmenu()
everytime I run it, I type in a number as the code asks but then, instead of asking if I want to return to the menu it just asks the question again and again, 'Guess the number of legs: '. I know this is something to do with my looping method but I do not understand and because of the integer setting I cannot just make another if, like so:
guessleg = int(input('Guess the number of legs: '))
if leg == guessleg:
print('True')
elif leg != guessleg:
print('False')
elif guessleg == 'back':
loop = 0
animalmenu()
And I do not see any other way of doing it as neither way seems to work? How would you suggest returning to animalmenu()?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如消息告诉您的那样,“back”不是整数,但您将其与已放入整数值的变量进行比较。具体来说,您的行:
将用户输入的整数值放入guessleg(或更正确地尝试)。
解决此问题的一种方法是在字符串变量中捕获用户的输入,将字符串与“ Back”进行比较,然后在需要时转换为整数。
另一种方法是将尝试/除外,除了围绕整数转换外,并继续进行整数检查转换是否成功,并在遇到异常(如果遇到异常)的情况下进行检查。如今,这可能是首选,我将其放入代码中:
As the message is telling you, 'back' is not an integer, but you are comparing it to a variable into which you have put an integer value. Specifically, your line:
puts an integer value into guessleg (or more properly tries to) from the user's input.
One approach to resolve this is to capture the user's input in a string variable, compare that string to 'back' first, and then convert to an integer if needed.
Another approach is to wrap a try/except around the integer conversion and proceed with the integer check if the conversion is successful and with the check against 'back' if the exception is encountered. This is probably preferred these days and I've put it into code:
因为您将输入转换为整数并将其存储到
guessleg
中,这意味着guessleg
也是一个整数。但是,'back'
是一个字符串。您无法将字符串与整数进行比较。3 == 'back'
没有任何意义。语法错误可能是由于您的缩进造成的。
更新:
如果你想返回到顶部菜单,你可以这样做:
再次更新:
我认为你不应该在这里使用 input() ,尝试 readline() 或 raw_input() 代替。
because the you convert your input into integer and store it into
guessleg
which meansguessleg
is also an integer. However,'back'
is a string. You can't compare the string with integer. the3 == 'back'
means nothing.and syntax error may because of your indent.
UPDATE:
if you want to return to the top menu, you can do something like:
UPDATE again:
I don't think you shall use input() here, try readline() or raw_input() instead.