泡菜错误:attributeError:可以在<模块上获取属性。 (内置)>
我在python的Pickle有问题
import time,sys,pickle
print("welcome to vom")
#what if rooms are mycelium that connect tothe vom pokeballs
with open('test.p', 'rb') as f:
rooms = pickle.load(f)
class Room(object):
def __init__(self,name):
self.name=name
self.intro=None
self.startroom=None
self.objects=[]
self.doors=[]
self.start=False
self.verbs={}
self.Class=None
class Object(object):
def __init__(self,name):
self.name=name
self.intro=None
self.verbs={}
class Key(object):
def __init__(self,name):
self.name=name
self.link=link
def gorl():
global player
while True:
print("what mode do you wanna be on the astral plane globally or local")
print("(type global or local for a response)")
myInput=input(">>> ")
if myInput=="global" or myInput=="local":
break
player.mode=myInput
def gorp():
global player
while True:
print("do you wanna play as a god or a player")
print("(type god or player for a response)")
myInput=input(">>> ")
if myInput=="god" or myInput=="player":
break
player.pmode=myInput
mykeyinstructions=["key"]
def addtoroom(numx):
global mykeyinstructions
global rooms
print("type the verbs you want to apply for your room (for example yo0u could type smell look hear) ")
myList=input(">>> ").split()
for x in myList:
print(x)
print("what do you want to happen when this verb is comitted?")
print("type add function if you want a local function otherwise type what you want the computer to say")
myInput=input(">>> ")
rooms[numx].verbs[x]=myInput
print("and then what? add objects and monsters and wizards to your room... by typing create x")
print("type help for more info type create to add an object")
while True:
myInput=input(">>>")
if myInput=="help":
print("you can create objects by typing create x")
print("if you wanna create a key type create key x")
print("type exit when you have finished creating your room")
instructions=myInput.split(" ")
myList=instructions[:]
del myList[0]
#print(instructions)
#print(instructions[0]=="create")
if instructions[0]=="create" and len(instructions)>1:
if instructions[1] not in mykeyinstructions:
print("you are forging a new object right now...")
rooms[numx].objects.append(Object(" ".join(myList)))
print("what should be the intro for your object?")
rooms[numx].objects[-1].intro=input(">>> ")
print("type the verbs you want to apply to your objects (for example yo0u could type smell look hear)")
myList=input(">>> ").split()
for x in myList:
print(x)
print("what do you want to happen when this verb is comitted?")
print("type add function if you want a local function otherwise type what you want the computer to say")
myInput=input(">>> ")
rooms[numx].objects[-1].verbs[x]=myInput
elif instructions[0]=="key":
del myList[1]
rooms[numx].objects.append(Key(" ".join(myList)))
if myInput=="exit":
print("you are quitting the game.")
pickle.dump(rooms,open("test.p","wb"))
sys.exit()
def createroom(start=False,status="local"):
global rooms
print("welcome to the abyss")
print("what you wanna do?")
if len(rooms)==0:
print("0>> ponder on the abyss of the energy")
print("1>> create a room")
print("type the number of the option")
myinput=input(">>> ")
while True:
if myinput=="0":
print("you ponder on the abyss of the energy...")
print("the aliens come down they are the energy they send you to a separate astral plane")
print("you win the game.")
sys.exit()
elif myinput=="1":
print("you create a room what would you like it to be officially called?")
rooms.append(Room(input(">>> ")))
rooms[-1].start=True
rooms[-1].status=status
break
else:
print("0>> ponder on the abyss of the room")
print("1>> create a room")
print("type the number of the option")
myinput=input(">>> ")
while True:
if myinput=="0":
print("you ponder on the abyss of the room...")
print("the aliens come down they are the energy they send you to a separate astral plane")
print("you win the game.")
sys.exit()
elif myinput=="1":
print("you create a room what would you like it to be officially called?")
rooms.append(Room(input(">>> ")))
break
print("what next? ")
time.sleep(1.0)
print("add an intro for your room")
intro=input(">>> ")
rooms[-1].intro=intro
addtoroom(-1)
class Player(object):
def __init__(self):
self.mode=None
self.pmode=None
self.intro=None
self.bronze=0
self.silver=0
self.gold=0
player=Player()
time.sleep(3.0)
gorl()
#anyone can edit rooms edit room mode needs to be created
#move up or down or left or right on the astral plane for standard connection of rooms
if player.mode=="global": #the difference between global or local is the fact that the doors of the mazes can link up to other player's stories in the mazes for global mode
print("welcome to the astral plane we will now log you onto the game")
gorp()
if len(rooms)==0:
print("the room is now empty all you see is the white abyss what do you wanna do?")
createroom(True,"global")
else:
print("do you wanna enter a astral plane or create a new one?")
print("1>> new story")
print("2>> log into an old story")
myinput=input(">>>")
if myinput=="1":
time.sleep(1.0)
#print("ok welcome to your new story...")
createroom()
else:
pass
if player.mode=="local":
print("welcome to story mode we will now log you onto the game")
gorp()
if len(rooms)==0:
print("the room is now empty all you see is the white abyss what do you wanna do?")
createroom(True)
else:
print("do you wanna enter an story or create a new one?")
print("1>> new story")
print("2>> log into an old story")
myinput=input(">>>")
if myinput=="1":
time.sleep(1.0)
print("ok welcome to your new story...")
createroom()
else:
pass #enter new story mode
myinput=input(">>>")
,然后我会遇到错误
Traceback (most recent call last):
File "C:/Users/macla/AppData/Local/Programs/Python/Python310/vom.py", line 8, in <module>
rooms = pickle.load(f)
AttributeError: Can't get attribute 'Room' on <module '__main__' (built-in)>
吗?
I am having a problem with pickle in python
import time,sys,pickle
print("welcome to vom")
#what if rooms are mycelium that connect tothe vom pokeballs
with open('test.p', 'rb') as f:
rooms = pickle.load(f)
class Room(object):
def __init__(self,name):
self.name=name
self.intro=None
self.startroom=None
self.objects=[]
self.doors=[]
self.start=False
self.verbs={}
self.Class=None
class Object(object):
def __init__(self,name):
self.name=name
self.intro=None
self.verbs={}
class Key(object):
def __init__(self,name):
self.name=name
self.link=link
def gorl():
global player
while True:
print("what mode do you wanna be on the astral plane globally or local")
print("(type global or local for a response)")
myInput=input(">>> ")
if myInput=="global" or myInput=="local":
break
player.mode=myInput
def gorp():
global player
while True:
print("do you wanna play as a god or a player")
print("(type god or player for a response)")
myInput=input(">>> ")
if myInput=="god" or myInput=="player":
break
player.pmode=myInput
mykeyinstructions=["key"]
def addtoroom(numx):
global mykeyinstructions
global rooms
print("type the verbs you want to apply for your room (for example yo0u could type smell look hear) ")
myList=input(">>> ").split()
for x in myList:
print(x)
print("what do you want to happen when this verb is comitted?")
print("type add function if you want a local function otherwise type what you want the computer to say")
myInput=input(">>> ")
rooms[numx].verbs[x]=myInput
print("and then what? add objects and monsters and wizards to your room... by typing create x")
print("type help for more info type create to add an object")
while True:
myInput=input(">>>")
if myInput=="help":
print("you can create objects by typing create x")
print("if you wanna create a key type create key x")
print("type exit when you have finished creating your room")
instructions=myInput.split(" ")
myList=instructions[:]
del myList[0]
#print(instructions)
#print(instructions[0]=="create")
if instructions[0]=="create" and len(instructions)>1:
if instructions[1] not in mykeyinstructions:
print("you are forging a new object right now...")
rooms[numx].objects.append(Object(" ".join(myList)))
print("what should be the intro for your object?")
rooms[numx].objects[-1].intro=input(">>> ")
print("type the verbs you want to apply to your objects (for example yo0u could type smell look hear)")
myList=input(">>> ").split()
for x in myList:
print(x)
print("what do you want to happen when this verb is comitted?")
print("type add function if you want a local function otherwise type what you want the computer to say")
myInput=input(">>> ")
rooms[numx].objects[-1].verbs[x]=myInput
elif instructions[0]=="key":
del myList[1]
rooms[numx].objects.append(Key(" ".join(myList)))
if myInput=="exit":
print("you are quitting the game.")
pickle.dump(rooms,open("test.p","wb"))
sys.exit()
def createroom(start=False,status="local"):
global rooms
print("welcome to the abyss")
print("what you wanna do?")
if len(rooms)==0:
print("0>> ponder on the abyss of the energy")
print("1>> create a room")
print("type the number of the option")
myinput=input(">>> ")
while True:
if myinput=="0":
print("you ponder on the abyss of the energy...")
print("the aliens come down they are the energy they send you to a separate astral plane")
print("you win the game.")
sys.exit()
elif myinput=="1":
print("you create a room what would you like it to be officially called?")
rooms.append(Room(input(">>> ")))
rooms[-1].start=True
rooms[-1].status=status
break
else:
print("0>> ponder on the abyss of the room")
print("1>> create a room")
print("type the number of the option")
myinput=input(">>> ")
while True:
if myinput=="0":
print("you ponder on the abyss of the room...")
print("the aliens come down they are the energy they send you to a separate astral plane")
print("you win the game.")
sys.exit()
elif myinput=="1":
print("you create a room what would you like it to be officially called?")
rooms.append(Room(input(">>> ")))
break
print("what next? ")
time.sleep(1.0)
print("add an intro for your room")
intro=input(">>> ")
rooms[-1].intro=intro
addtoroom(-1)
class Player(object):
def __init__(self):
self.mode=None
self.pmode=None
self.intro=None
self.bronze=0
self.silver=0
self.gold=0
player=Player()
time.sleep(3.0)
gorl()
#anyone can edit rooms edit room mode needs to be created
#move up or down or left or right on the astral plane for standard connection of rooms
if player.mode=="global": #the difference between global or local is the fact that the doors of the mazes can link up to other player's stories in the mazes for global mode
print("welcome to the astral plane we will now log you onto the game")
gorp()
if len(rooms)==0:
print("the room is now empty all you see is the white abyss what do you wanna do?")
createroom(True,"global")
else:
print("do you wanna enter a astral plane or create a new one?")
print("1>> new story")
print("2>> log into an old story")
myinput=input(">>>")
if myinput=="1":
time.sleep(1.0)
#print("ok welcome to your new story...")
createroom()
else:
pass
if player.mode=="local":
print("welcome to story mode we will now log you onto the game")
gorp()
if len(rooms)==0:
print("the room is now empty all you see is the white abyss what do you wanna do?")
createroom(True)
else:
print("do you wanna enter an story or create a new one?")
print("1>> new story")
print("2>> log into an old story")
myinput=input(">>>")
if myinput=="1":
time.sleep(1.0)
print("ok welcome to your new story...")
createroom()
else:
pass #enter new story mode
myinput=input(">>>")
then I get the error
Traceback (most recent call last):
File "C:/Users/macla/AppData/Local/Programs/Python/Python310/vom.py", line 8, in <module>
rooms = pickle.load(f)
AttributeError: Can't get attribute 'Room' on <module '__main__' (built-in)>
can anyone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在定义所有课程之前,您正在加载泡菜。
Pickle.load
将在Python有机会解析和执行其余类定义之前发生。通常,您希望避免在模块级别运行任何真实代码;取而代之的是,如果__NAME__ ==“ __ -Main __” block,请在
的底部运行这种“主程序”代码。因此:
You're loading the pickle before defining all of your classes. The
pickle.load
will occur before Python has had a chance to parse and execute the rest of your class definitions.Typically, you want to avoid running any real code at module level; instead, run that sort of "main program" code at the bottom in an
if __name__ == "__main__"
block. Thus: