python - 如何修复我的 Python 函数,使其返回输入提示?
问题描述
我有一个菜单功能和选择功能都有效。有 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
该变量。choice
您必须在以下行之前捕获您的输入:if 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:
...
推荐阅读
- sql-server - Mybatis Generator中生成Instant的SQL Server类型
- python - 标签编码然后估算缺失然后逆编码
- flutter - 将数据库条目保存到文本文件(颤振)
- react-native - 我应该修复仅在远程调试器中的警告吗?| 反应原生
- r - 如何在 R 中实现嵌套哈希映射?
- vue.js - 无效的路由组件
- php - 在产品保存时创建 WooCommerce 产品变体
- javascript - TypeError:尝试编辑时无法读取未定义的属性“获取”
- python - 我的服务器没有响应,它挂了,没有返回任何东西
- android - Android:如何有条件地显示字符串资源中的浮点部分?