プログラミング初心者なのでお手柔らかにお願いします。過去数日かそこらで、私はそれを機能させようとしてコードをいじり、スタックオーバーフローの内外で多くの調査を行いました(まあ、私にとっては多くのことだと思います)。何十億回も質問を繰り返すことはありません。また、貧弱でおそらく読みにくいコードについてお詫び申し上げます。クラスとメソッドの使用について独学するためにこれを行っています。
問題は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 型なのはなぜですか?