我有如下因素代碼:我不懂代碼的行爲
from random import randint
from medical_room import *
from Library import *
from basement import *
from End import *
class start_Game(object):
def __init__(self):
print "You landed on planet and see three rooms."
print "You approach and see that you need to enter password..."
self.door=raw_input("Pick number of door>>>")
self.password=('%d')%(randint(1,9))
self.entered_password=int(raw_input("Enter password of one digit>>>"))
self.ROOMs={'1':Medical_room,'2':Library,'3':basement,'4':End}
while True:
# break
room=self.ROOMs[self.door]
# print room()
self.door=room()
a=start_Game()
當被問及門號我選擇「1」和類Medical_room
啓動(類的代碼如下):
class Medical_room(object):
def __init__(self):
self.play()
def play(self):
print "Medical_room plays"
return '2'
但我不能切換到Library
類,因爲出現錯誤:
room=self.ROOMs[self.door]
KeyError: <medical_room.Medical_room object at 0x0000000002906978>
對我來說一切都好的,但Python不喜歡我的'偉大的邏輯'。請幫忙。
Python不是Java。你不必把所有東西都變成一堂課。具體來說,如果一個類沒有'__init__'以外的方法,它可能會被定義爲一個函數。 – chepner
請擺脫那些* - 進口。你在混淆你的名字空間。 – Matthias