开发者

Returning to menu after command? Python

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()?


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()


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜