【问题标题】:Math Menu Loop return数学菜单循环返回
【发布时间】:2020-10-11 20:31:58
【问题描述】:

该函数不会返回选项,我多次运行代码并没有发现错误。看看你们能不能发现我在网上到处找。

def menu():
    print("1. Addition")
    print("2. Subtraction")
    print("3. Multiplication")
    print("4. Division")
    
    
def main(option):
    option = input("Enter your selection: ")
    
    while option != 1 or 2 or 3 or 4:
        count = 0 #controls how many menu() is printed
        print(menu(), count)
        count = +1
        option = input("Enter your selection: ")
        return option
        
    
main()

控制台输出

TypeError: main() missing 1 required positional argument: 'option'

【问题讨论】:

  • 你定义了 main(option) 但你没有通过 main 传递一个 'option' 参数。

标签: python-3.x return


【解决方案1】:

试试这样:

menu()

choice = int(input("Enter input: ")) # get user input and cast it to int

main(choice) # requires a number to be passed to it

或者你可以修改 main 不带选项:

def main(): # it no longer takes a parameter
    option = input("Enter your selection: ")
    
    while option != 1 or 2 or 3 or 4:
        count = 0 #controls how many menu() is printed
        print(menu(), count)
        count = +1
        option = input("Enter your selection: ")
        return option
        
    
main() # this will work now

【讨论】:

    【解决方案2】:

    你调用了一个 main 函数而没有调用传入参数。

    您的函数 main 是一个接受变量选项的函数

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-20
      • 2014-04-20
      • 1970-01-01
      相关资源
      最近更新 更多