python - 为什么第一部分代码不断重复?
问题描述
import sys
我无法检查此导入是否有效,因为代码本身不起作用
options_list = [1,2,3]
def options(options_list):
print ("\n1. Hypotenuse solver \n2. Scat jazz \n3. Exit")
print ("Enter the number of the option you would like")
option = input()
return option
option = options(options_list)
while option not in options_list:
option = options(options_list)
if option in options_list:
if option == 1:
print ("...")
稍后我会添加这个,希望
elif option == 2:
print ("Scoobidibahbahbah")
elif option == 3:
sys.exit
这是一个学校项目,一个带有菜单的斜边求解器
解决方案
在从 options 方法返回之前尝试将 option 转换为 int 。
而不是return option
,尝试return int(option)
input() 将值读取为字符串。当您进行比较时option==1
,将比较字符串和整数,并且没有其他语句可以处理这种情况。此外,您拥有的选项始终不在options_list
. 这就是为什么您的代码继续无限循环而不打印任何内容的原因。
工作正常,import
因为sys
它是一个可靠的 python 包。
推荐阅读
- excel - 如何使用过滤器数据进行 Vlookup
- mongodb - 如何过滤mongodb聚合中的主数组
- matlab - 从 OpenFace 数据中检测头部点头
- postgresql - 函数内部的循环作为单个事务运行并读取单个快照的数据
- android - Android 自定义操作栏/工具栏/标签栏
- macos - 使用自制软件安装 taurus
- android - 无法使用 Hilt 创建带有构造函数参数的 ViewModel 实例
- r - 如何通过比较两个公共列来识别仅存在于两个数据集中之一中的行?
- snowflake-cloud-data-platform - 雪花中用退格符括起来的字段的文件格式
- javascript - 服务工作者在 1 分钟后不发送任何数据