尝试运行方法时,请获取'NoneType is not callable'



我刚开始编程,所以请温柔一点。在过去的几天里,我一直在摆弄代码,试图让它工作,我在stackoverflow内外做了很多研究(嗯,我想对我来说很多),所以希望我不会重复一个问题十亿次。另外,我为那些可怜的、可能难以阅读的代码表示歉意。我这样做是为了教自己如何使用类和方法。

问题是在rm1方法中的return self.rm3和rm3本身的代码之间的某个地方。

我的代码是:

from sys import exit
from random import randint
def death():
    print 'You are "dead", I guess.'
    exit(0)
def inp(prompt='What do you do? '):
    to_return = raw_input(prompt).lower()
    if to_return == 'q' or to_return == 'quit':
        print "Bye now~"
        exit(0)
    else:
        return to_return
class Rooms(object):
    def __init__(self):
        current_room = self.rm1
        self.items = []
        while True:
            current_room = current_room()
    def rm1(self):
        print "=== You are in Room 1 ==="
        print "You are in a dark room."
        print "Obvious exits are east and south."
        while True:
            choice = inp()
            if choice == "east":
                return self.rm2
            elif choice == "south":
                return self.rm3
            elif choice == "look":
                return self.rm1
    def rm2(self):
        print "=== You are in Room 2 ==="
        if "haircomb" not in self.items:
            print "There is a high-tech looking keypad on the southernly wall,"
            print "next to the door."
            print "Upon closer inspection there are not numbers, but three"
            print "letters on the keypad: A, M, and Z."
            print "Do you try to guess the code?"
        print "Obvious exits are west and south."
        while True:
            choice = inp()
            if "guess code" in choice and "haircomb" not in self.items:
                return self.rm2guess
            elif choice == "west":
                return self.rm1
            elif choice == "south" and "haircomb" not in self.items:
                print "The door is firmly locked."
            elif choice == "south" and "haircomb" in self.items:
                return self.rm4
            elif choice == "look":
                return self.rm2
    def rm2guess(self):
        correct_answer = randint(1,3)
        guesses_left = 2
        print "You approach the keypad."
        while True:
            guess = inp("Which key do you press? ").lower()
            if guess == "a" or guess == "m" or guess == "z":
                if guess == "a":
                    guess = 1
                elif guess == "m":
                    guess = 2
                elif guess == "z":
                    guess = 3
                if guess == correct_answer:
                    print "The machine whirrs and then beeps."
                    print "The southernly door seems to be open now."
                    print "Suddenly, a haircomb falls from the bottom of the "
                    print "keypad. You take it."
                    self.items.append("haircomb")
                    return self.rm2
                elif guess != correct_answer and guesses_left > 0:
                    print "The machine whirrs and clicks %s times." 
                                                        % guesses_left
                    guesses_left -= 1
                elif guess != correct_answer and guesses_left == 0:
                    print "An alarm goes off and the police pick you up."
                    print "You are put on death row, because you live under a"
                    print "horrible dictatorship."
                    death()
            else:
                print "That ain't no key on the pad, yo."
    def rm3(self):
        def __init__(self):
            if "coin" not in self.items:
                print "You come into a room. A man stands at the doorway."
                print "He tells you that in order to pass the door, you need to"
                print "guess which hand holds the coin."
                print "Do you try to guess?"
            print "Obvious exits are east and north."
            while True:
                choice = inp()
                if "guess" in choice:
                    return self.rm3guess
                elif choice == "north":
                    return self.rm1
    def rm4(self):
        def __init__(self):
            print "room 4"
            raw_input
            return self.rm4
    def rm5(self):
        pass
    def rm6(self):
        pass
game = Rooms()

运行并输入'south'时的输出为:

=== You are in Room 1 ===
You are in a dark room.
Obvious exits are east and south.
What do you do? south
Traceback (most recent call last):
  File "ta.py", line 141, in <module>
    game = Rooms()
  File "ta.py", line 44, in __init__
    current_room = current_room()
TypeError: 'NoneType' object is not callable
shell returned 1

我不知道这样的错误是怎么发生的?为什么方法rm3是一个None类型时,它显然在类中定义?

为什么要在函数内部添加__init__函数?它是要添加到类中的构造函数。消除这些问题就解决了。我可以进入south并进一步。

from sys import exit
from random import randint
def death():
    print 'You are "dead", I guess.'
    exit(0)
def inp(prompt='What do you do? '):
    to_return = raw_input(prompt).lower()
    if to_return == 'q' or to_return == 'quit':
        print "Bye now~"
        exit(0)
    else:
        return to_return
class Rooms(object):
    def __init__(self):
        current_room = self.rm1
        self.items = []
        while True:
            current_room = current_room()
    def rm1(self):
        print "=== You are in Room 1 ==="
        print "You are in a dark room."
        print "Obvious exits are east and south."
        while True:
            choice = inp()
            if choice == "east":
                return self.rm2
            elif choice == "south":
                return self.rm3
            elif choice == "look":
                return self.rm1
    def rm2(self):
        print "=== You are in Room 2 ==="
        if "haircomb" not in self.items:
            print "There is a high-tech looking keypad on the southernly wall,"
            print "next to the door."
            print "Upon closer inspection there are not numbers, but three"
            print "letters on the keypad: A, M, and Z."
            print "Do you try to guess the code?"
        print "Obvious exits are west and south."
        while True:
            choice = inp()
            if "guess code" in choice and "haircomb" not in self.items:
                return self.rm2guess
            elif choice == "west":
                return self.rm1
            elif choice == "south" and "haircomb" not in self.items:
                print "The door is firmly locked."
            elif choice == "south" and "haircomb" in self.items:
                return self.rm4
            elif choice == "look":
                return self.rm2
    def rm2guess(self):
        correct_answer = randint(1,3)
        guesses_left = 2
        print "You approach the keypad."
        while True:
            guess = inp("Which key do you press? ").lower()
            if guess == "a" or guess == "m" or guess == "z":
                if guess == "a":
                    guess = 1
                elif guess == "m":
                    guess = 2
                elif guess == "z":
                    guess = 3
                if guess == correct_answer:
                    print "The machine whirrs and then beeps."
                    print "The southernly door seems to be open now."
                    print "Suddenly, a haircomb falls from the bottom of the "
                    print "keypad. You take it."
                    self.items.append("haircomb")
                    return self.rm2
                elif guess != correct_answer and guesses_left > 0:
                    print "The machine whirrs and clicks %s times." 
                                                        % guesses_left
                    guesses_left -= 1
                elif guess != correct_answer and guesses_left == 0:
                    print "An alarm goes off and the police pick you up."
                    print "You are put on death row, because you live under a"
                    print "horrible dictatorship."
                    death()
            else:
                print "That ain't no key on the pad, yo."
    def rm3(self):
        if "coin" not in self.items:
            print "You come into a room. A man stands at the doorway."
            print "He tells you that in order to pass the door, you need to"
            print "guess which hand holds the coin."
            print "Do you try to guess?"
        print "Obvious exits are east and north."
        while True:
            choice = inp()
            if "guess" in choice:
                return self.rm3guess
            elif choice == "north":
                return self.rm1
    def rm4(self):
        print "room 4"
        raw_input
        return self.rm4
    def rm5(self):
        pass
    def rm6(self):
        pass
game = Rooms()

您的rm3rm4函数没有任何用处,因为出于某些原因,您在每个函数中声明了嵌套函数__init__

因此,当您调用rm3()时,它不会做您所期望的,它声明局部嵌套函数__init__,并返回None,因为它没有"自己"的主体。

相关内容

最新更新