首页 > 解决方案 > 为什么第一部分代码不断重复?

问题描述

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

这是一个学校项目,一个带有菜单的斜边求解器

标签: pythonfunctionpython-import

解决方案


在从 options 方法返回之前尝试将 option 转换为 int 。

而不是return option,尝试return int(option)

input() 将值读取为字符串。当您进行比较时option==1,将比较字符串和整数,并且没有其他语句可以处理这种情况。此外,您拥有的选项始终不在options_list. 这就是为什么您的代码继续无限循环而不打印任何内容的原因。

工作正常,import因为sys它是一个可靠的 python 包。


推荐阅读