泡菜错误:attributeError:可以在<模块上获取属性。 (内置)>

发布于 2025-01-21 09:21:59 字数 7614 浏览 2 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄凉少年不暖心 2025-01-28 09:21:59

在定义所有课程之前,您正在加载泡菜Pickle.load将在Python有机会解析和执行其余类定义之前发生。

通常,您希望避免在模块级别运行任何真实代码;取而代之的是,如果__NAME__ ==“ __ -Main __” block,请在的底部运行这种“主程序”代码。因此:

import ...

class Room: ...

class Object: ...

...

if __name__ == "__main__":
    print("welcome to vom")
    
    with open('test.p', 'rb') as f:
        rooms = pickle.load(f)

    player=Player()

    time.sleep(3.0)

    gorl()
    ...

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:

import ...

class Room: ...

class Object: ...

...

if __name__ == "__main__":
    print("welcome to vom")
    
    with open('test.p', 'rb') as f:
        rooms = pickle.load(f)

    player=Player()

    time.sleep(3.0)

    gorl()
    ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文