命令后返回菜单? Python

发布于 2024-10-28 16:27:54 字数 1895 浏览 3 评论 0原文

我知道我所有的问题都很简单,但我是初学者,所以这里是...... 在大家的帮助下我一直在开发猜数字的东西,但我想然后返回到刚刚留下的菜单。这是代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌路黄昏 2024-11-04 16:27:55

正如消息告诉您的那样,“back”不是整数,但您将其与已放入整数值的变量进行比较。具体来说,您的行:

guessleg = int(input('Guess the number of legs: '))

将用户输入的整数值放入guessleg(或更正确地尝试)。

解决此问题的一种方法是在字符串变量中捕获用户的输入,将字符串与“ Back”进行比较,然后在需要时转换为整数。

另一种方法是将尝试/除外,除了围绕整数转换外,并继续进行整数检查转换是否成功,并在遇到异常(如果遇到异常)的情况下进行检查。如今,这可能是首选,我将其放入代码中:

inp_val = raw_input('Guess the number of legs: ')
try:
    guess_num = int(inp_val)
    if guess_num == leg:
        print('True')
    else:
        print('False')
except ValueError:
    if inp_val == 'back':
        loop = 0
    else:
        print 'Invalid entry'
    animalmenu()

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:

guessleg = int(input('Guess the number of legs: '))

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:

inp_val = raw_input('Guess the number of legs: ')
try:
    guess_num = int(inp_val)
    if guess_num == leg:
        print('True')
    else:
        print('False')
except ValueError:
    if inp_val == 'back':
        loop = 0
    else:
        print 'Invalid entry'
    animalmenu()
成熟稳重的好男人 2024-11-04 16:27:55

因为您将输入转换为整数并将其存储到 guessleg 中,这意味着 guessleg 也是一个整数。但是,'back' 是一个字符串。您无法将字符串与整数进行比较。 3 == 'back' 没有任何意义。
语法错误可能是由于您的缩进造成的。

更新:

如果你想返回到顶部菜单,你可以这样做:

def animalmenu():
  while True:
    print your menu here
    and do something....
    while ...:
      get input and do something...
      if get the input of 'back to menu':
        break

再次更新:

我认为你不应该在这里使用 input() ,尝试 readline() 或 raw_input() 代替。

because the you convert your input into integer and store it into guessleg which means guessleg is also an integer. However, 'back' is a string. You can't compare the string with integer. the 3 == '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:

def animalmenu():
  while True:
    print your menu here
    and do something....
    while ...:
      get input and do something...
      if get the input of 'back to menu':
        break

UPDATE again:

I don't think you shall use input() here, try readline() or raw_input() instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文