NameError: global name 'object' is not defined Python
NameError: global name 'object' is not defined Python
我已经 Python 几个月了,但在定义函数时遇到了问题。我只是在创建一个小型 CYOA 迷宫,其中有多个房间,玩家必须来回走动。它适用于某些功能,但我收到 NameError: global name 'object' is not defined 错误。
#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 然后我输入一号门到达 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(">")
我已经 Python 几个月了,但在定义函数时遇到了问题。我只是在创建一个小型 CYOA 迷宫,其中有多个房间,玩家必须来回走动。它适用于某些功能,但我收到 NameError: global name 'object' is not defined 错误。
#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 然后我输入一号门到达 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(">")