0

我正在编写一个程序,它是石头剪刀布游戏变体的模拟游戏。在我的主函数中,我正在输出一个接口,然后尝试调用其中定义的一个方法,该方法充当 switch 语句。我得到:“makeSel() 缺少 1 个必需的位置参数:'self'” TypeErrors 我已经在网上搜索了任何提示,但找不到任何有帮助的提示。我试图将“makeSel()”更改为“Main.makeSel()”,但它只是让我知道 main 是未定义的。


class Main:

    def makeSel(self):
        selection = input()
        if(selection == 1):
            return StupidBot('StupidBot')
        elif(selection == 2):
            return RandomBot('RandomBot')
        elif(selection == 3):
            return IterativeBot('IterativeBot')
        elif(selection == 4):
            return LastPlayBot('LastPlayBot')
        elif(selection == 5):
            return MyBot('MyBot')
        elif(selection == 6):
            return HumanPlayer('HumanPlayer')
        else:
            print('Invalid selection, please try again.  Enter 1, 2, 3, 4, 5, or 6')
            makeSel()

    print('')
    print('Welcome to my Rock-Paper-Scissors-Lizard-Spock game!')
    print('')
    print('Please select two players, enter 1, 2, 3, 4, 5, or 6')
    print('  (1) -> StupidBot')
    print('  (2) -> RandomBot')
    print('  (3) -> IterativeBot')
    print('  (4) -> LastPlayBot')
    print('  (5) -> MyBot')
    print('  (6) -> HumanPlayer')

    p1 = makeSel()
    p2 = makeSel()

我希望有人能够对我遇到的问题有所了解。

4

1 回答 1

1

要修复直接但,请更改makeSel()self.makeSel(). self与其他一些语言不同,它不会隐式传递给 Python 中的方法调用。你的意思也是return self.makeSel(),我想。

另外,我认为你想要一个循环mainSel()。你递归调用mainSel()which 工作很像循环,但会填满调用堆栈。

class Main:

    def makeSel(self):
        while True
            selection = input()
            if(slection == 1):
                return StupidBot('StupidBot')
            elif(slection == 2):
                return RandomBot('RandomBot')
            elif(selection == 3):
                return IterativeBot('IterativeBot')
            elif(selection == 4):
                return LastPlayBot('LastPlayBot')
            elif(selection == 5):
                return MyBot('MyBot')
            elif(selection == 6):
                return HumanPlayer('HumanPlayer')
            else:
                print('Invalid selection, please try again.  Enter 1, 2, 3, 4, 5, or 6')
于 2013-03-24T22:59:05.967 回答