我是编程新手,所以请温柔一点。在过去几天左右的时间里,我弄乱了代码以使其正常工作,并且我在stackoverflow内外做了很多研究(好吧,我猜对我来说很多),所以希望我不会第十亿次重复一个问题。另外,我为穷人和可能难以阅读的代码道歉。我这样做是为了自学使用类和方法。
问题出return self.rm3
在 rm1 方法和 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 类型?