python - 如何修复我的 Python 函数,使其返回时带有输入提示?

标签 python function

我有一个菜单功能和选择功能,两者都有效。有 3 种菜单选择。 1 和 3 曾一度工作正常。 2 从来没有。我不知道我做了什么把它搞砸了,但是当我运行模块通过 IDLE 进行测试时,它在第一次提示输入我的菜单选项编号之后就不再工作了。它应该完成 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(),而是用提示结果覆盖它。

下面我将纠正这些问题。

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

关于python - 如何修复我的 Python 函数,使其返回时带有输入提示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57814588/

相关文章:

javascript - 我可以命名一个 JavaScript 函数并立即执行吗?

python - Voronoi图的轮廓坐标

javascript - 跳过按钮 - 如何用另一个功能停止一个功能

python - 使用 shutil 在 python 中移动文件

python - 在 Python 中使用列表和字典通过数字比较优化循环效率

c - 如何处理错误 "control may reach end of non void function"?

javascript - 如果 JavaScript 中的一个函数有多个函数定义,为什么最后一个实现会优先?

python - while循环运行一次?

python - 使用 Pyomo 的旅行推销员

python -/etc/nginx/sites-enabled/django 中的无效参数 server_name