如何修复Python函数,使其返回输入提示?

如何修复Python函数,使其返回输入提示?,python,function,Python,Function,我有一个菜单功能和选择功能,两者都起作用。有3个菜单选项。1和3在某一点上工作正常。2从来没有。我不知道我是怎么搞砸的,但当我在空闲状态下运行模块进行测试时,在第一次提示输入菜单选项编号后,它就不会工作了。它应该完成一个if语句,然后重新启动 我不知道还能尝试什么。我希望我知道我改变了什么,把事情搞砸了 tribbles = 1 modulus = 2 closer= 3 def menu(): print(' -MENU-') print('1: Tribbles Ex

我有一个菜单功能和选择功能,两者都起作用。有3个菜单选项。1和3在某一点上工作正常。2从来没有。我不知道我是怎么搞砸的,但当我在空闲状态下运行模块进行测试时,在第一次提示输入菜单选项编号后,它就不会工作了。它应该完成一个if语句,然后重新启动

我不知道还能尝试什么。我希望我知道我改变了什么,把事情搞砸了

tribbles = 1
modulus = 2
closer= 3

def menu():
    print('    -MENU-')
    print('1: Tribbles Exchange')
    print('2: Odd or Even?')
    print("3: I'm not in the mood...")

menu()
def choice():
    choice = int(input('\n Enter the number of your menu choice: ')


if choice == tribbles:
    bars = int(input('\n How many bars of gold-pressed latinum do you have? '))
    print('\n You can buy ',bars * 5000 / 1000,' Tribbles.')
    menu()
    choice()
elif choice == modulus:
    num = int(input('\n Enter any number:'))
    o_e = num % 2
    if num == 0:
        print(num,' is an even number')
    elif num == 1:
        print(num,' is an odd number')
    menu()
    choice()
elif choice == closer:
    print('\n Thanks for playing!')
    exit()
else:
    print('Invalid entry. Please try again...')
    menu()
    choice()
print(' ')
choice = int(input('\n Enter the number of your menu choice: '))

我希望它返回字符串加上所有公式结果,然后再次询问,除非选择选项3并执行exit()。但是,在第一次输入后,它会返回“输入菜单选项的编号:”并在第二次提示中选择任何其他选项后返回空白。f

在检查
choice
的值之前,不会声明变量
choice
。如果choice==tribbles:,则必须在以下行之前捕获您的输入:
。您只定义了一个函数,它甚至不返回您选择的值或设置全局变量

试试这个:

def menu():
    print('    -MENU-')
    print('1: Tribbles Exchange')
    print('2: Odd or Even?')
    print("3: I'm not in the mood...")

menu()
choice = int(input('\n Enter the number of your menu choice: '))

if choice == tribbles:
...
第一件事

最好在文件顶部定义所有函数,并在底部调用这些函数!第二,你的缩进是不正确的,我假设这是在你粘贴到这里之后发生的。最后,您从未实际调用函数
choice()
,而是使用提示结果覆盖它

下面我将纠正这些问题

tribbles = 1
modulus = 2
closer= 3

def menu():
    print('    -MENU-')
    print('1: Tribbles Exchange')
    print('2: Odd or Even?')
    print("3: I'm not in the mood...")
    choice() #added call to choice here because you always call choice after menu

def choice():
    my_choice = int(raw_input('\nEnter the number of your menu choice: ')) #you were missing a ) here! and you were overwriting the function choice again
    #changed choice var to my_choice everywhere

    if my_choice == tribbles:
        bars = int(raw_input('\nHow many bars of gold-pressed latinum do you have? '))
        print('\n You can buy ',bars * 5000 / 1000,' Tribbles.')
        menu()
    elif my_choice == modulus:
        num = int(raw_input('\n Enter any number:'))
        o_e = num % 2
        if num == 0:
            print(num,' is an even number')
        elif num == 1:
            print(num,' is an odd number')
        menu()
    elif choice == closer:
        print('\n Thanks for playing!')
        exit()
    else:
        print('Invalid entry. Please try again...')
        menu()
    print(' ')


if __name__ == "__main__": #standard way to begin. This makes sure this is being called from this file and not when being imported. And it looks pretty!
    menu()

出于某种原因,它在运行时会出现一个错误:“无效语法”。它指向“:”在“if choice==tribbles”之后,在输入()之后缺少一个括号。。。我编辑了这篇博文,谢谢你的回答。我最终牺牲了回环以允许更多的选择。TheLazyScripter的解决方案允许这样做,但我不知所措。如果你不知道,我对编程是全新的。我现在在上编程逻辑基础课。