【问题标题】:Python class argument errorPython类参数错误
【发布时间】:2015-08-07 12:28:44
【问题描述】:

我正在创建一个菜单,但我遇到了一个错误,需要一些帮助,因为我不知道什么是错误的或如何修复它,这段代码说我正在输入一个参数,但我没有输入了一个参数。

class menu(object):

    def print_menu():
    # menu options    
        print "Main Menu:"
        print "Start"
        print "Quit"

    def user_menu():
    # users input
        menu_choice = raw_input('> ')

        if menu_choice == 'start':
            start()
        #does nothing as of yet
        elif menu_choice == 'quit':
            raise SystemExit

def start():
    pass

#initialising main menu
main = menu()

def start_up()

    main.print_menu()
    #first attempt
    main.user_menu()
    #second attempt
    main.user_menu()
    #third attempt
    main.user_menu()
    # start again to show the menu options
    start_up()


start_up()

请帮忙,这是我运行脚本时在控制台中发生的最近一次调用的回溯错误

Traceback (most recent call last):
 File "Engine.py", line 38, in <module>
    start_up()
  File "Engine.py", line 27, in start_up
   main.print_menu()
TypeError: print_menu() takes no arguments (1 given)

【问题讨论】:

    标签: python function class arguments


    【解决方案1】:

    您忘记添加 self 作为参数。

    所以它必须看起来像这样:

    class menu(object):
    
        def print_menu(self):
            # menu options    
            print "Main Menu:"
            print "Start"
            print "Quit"
    
        def user_menu(self):
            # users input
            menu_choice = raw_input('> ')
    
            if menu_choice == 'start':
                start()
            #does nothing as of yet
            elif menu_choice == 'quit':
                raise SystemExit
    

    另外,我不确定是否需要在这里使用class。如果我是你,我会摆脱 menu 类并使用 just leave 那些方法。

    【讨论】:

    • 谢谢!!!我是新手,所以似乎我对最简单的错误很粗心
    • 哦,这里的课程是我正在做的项目所需要的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-13
    • 2011-02-07
    • 2023-04-03
    • 2015-01-15
    • 2021-02-18
    • 2014-03-28
    • 2021-12-31
    相关资源
    最近更新 更多