簡體   English   中英

NameError:全局名稱“對象”未定義Python

[英]NameError: global name 'object' is not defined Python

我已經使用Python幾個月了,在定義函數時遇到了麻煩。 我只是在創建一個小的CYOA迷宮,那里有多個房間,玩家必須來回走動。 它可以與某些功能一起使用,但是我遇到了NameError:全局名稱'object'未定義錯誤。

#Main room
def big_room():
    print """You arrive inside a huge square room. You are presented with 
3 giant wooden doors. Which door do you choose?Or you can Leave."""


    room = raw_input(">")

    if room == "door one":
        print"You open door one and walk in."
        room_one()
    elif room == "door two":
        room_two()
    elif room == "door three":
        print """You go toward door three and attempt to pull it.
The door opens loudly and you walk in."""
        room_three()
    elif room == "Leave":
        dead("You decide to walk away and get eaten by a Jaguar")
    else:
        print "What are you saying?." 

def room_one():
    print """The room has one torch lighting the room with a faint glow. You are presented
with another three doors. Which way do you go? or go back"""


    room1 == raw_input(">")

    if room1 == "door one":
        print "Test"
    elif room1 == "door two":
        print"Test"
    elif room1 == "door c":
        hole()
    elif room1 == "go back":
        big_room()
    else:
        print "test me"

def hole():
    print "You take a step forward and the floor below you caves in; you fall in and die."
    dead()


def dead(why):
    print why,"Game over!"
    exit(0)


raw_input("Press Enter to Continue")

def start():
    print """After a long time of walking through a thick jungle,
you arrive at a temple covered in foliage and a narrow entrance.
go inside. or Leave"""

    while True:
        temple = raw_input(">")

        if temple =="go inside":
            big_room()
        elif temple == "Leave":
            dead("You decide to walk away and get eaten by a Jaguar")
        else:
            print "What are you saying?"

start()

運行它時,我可以進入big_room,然后鍵入門1進入door_one()。 我假設它應該到達def room_one()並從那里運行。 我知道了

The room has one torch lighting the room with a faint glow. You are presented
with another three doors. Which way do you go? or go back
Traceback (most recent call last):
  File "flee.py", line 78, in <module>
    start()
  File "flee.py", line 72, in start
    big_room()
  File "flee.py", line 21, in big_room
    room_one()
  File "flee.py", line 38, in room_one
    room1 == raw_input(">")
NameError: global name 'room1' is not defined

我確定像在big_room()中那樣輸入一些輸入時,我會定義它。 任何幫助將不勝感激。 謝謝

您的代碼中有這一行:

room1 == raw_input(">")

你是這個意思嗎

room1 = raw_input(">")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM