我遇到了问题。快速的回应将不胜感激!我的程序失败了IF条件,因为我的函数没有为我正确更改全局变量。例如,他们应该能够向南走一把钥匙。有了这把钥匙,他们可以往东走,打开一个锁着的抽屉。除了......他们没有通过if检查能够打开抽屉。
提前致谢!有问题的代码块应该在下面!
def south():
print ("You can see a key just lying there on the table! What luck!")
choice = raw_input("You better TAKE that!")
if choice == 'TAKE' :
print "You took the key!"
return Key1 == 1, moverooms()
else:
print "You didn't take the key to freedom!?"
south()
def east():
print("You can see a drawer here! Wonder what is inside?")
choice = raw_input("You can MOVEROOMS, or try to USE the drawer and TAKE what's inside...\n ")
if choice == 'USE' :
print "You try to open the drawer... \n"
if Key1 == 1 :
print "You use the key to open the drawer and find a flashlight inside! Better TAKE it!"
Drawer == 1
east()
else:
print ("It's locked! Better find a key...\n")
east()
答案 0 :(得分:0)
您真的不想使用全局变量,但如果必须,您的问题似乎是您未在Key1 = 1
条件中分配TAKE
,而是返回True
或False
根据其是否已具有该值(Key1==1
)。请注意,您需要在return
之前设置它。
请注意,如果您要执行此操作(不要),则需要在global Key
函数的顶部声明south()
。
要避免全局变量,请从Key1
返回south
的值并将其传递给east
:
def south():
print ("You can see a key just lying there on the table! What luck!")
choice = raw_input("You better TAKE that!")
if choice == 'TAKE' :
print "You took the key!"
Key1 = 1
else:
print "You didn't take the key to freedom!?"
Key1 = 0
return Key1
def east(Key1):
print("You can see a drawer here! Wonder what is inside?")
choice = raw_input("You can MOVEROOMS, or try to USE the drawer and TAKE what's inside...\n ")
if choice == 'USE' :
print "You try to open the drawer... \n"
if Key1 == 1 :
print "You use the key to open the drawer and find a flashlight inside! Better TAKE it!"
Drawer = 1
return Drawer
else:
print ("It's locked! Better find a key...\n")
Drawer = 0
return Drawer
但是,您必须自己处理对south
和east
的调用的逻辑。
答案 1 :(得分:0)
这可能有点矫枉过正,但理想情况下,你会按照这些方针做点什么:
class Character(object):
"""
This class represents the player.
It keeps track of found items, opened drawers, etc...
"""
def __init__(self):
# game start: Key not found, drawer not opened.
self.has_key= False
self.has_opened_drawer= False
def go_south(self):
print "You can see a key just lying there on the table! What luck!"
choice = raw_input("You better TAKE that!\n")
if choice == 'TAKE' :
print "You took the key!"
self.has_key= True
else:
print "You didn't take the key to freedom!?"
def go_east(self):
print "You can see a drawer here! Wonder what is inside?"
choice = raw_input("You can MOVEROOMS, or try to USE the drawer and TAKE what's inside...\n")
if choice == 'USE':
print "You try to open the drawer... \n"
if self.has_key:
print "You use the key to open the drawer and find a flashlight inside! Better TAKE it!"
self.has_opened_drawer= True
else:
print "It's locked! Better find a key...\n"
def input_loop(self):
while True:
choice= raw_input('Do you want to go SOUTH or EAST?\n')
if choice=='SOUTH':
self.go_south()
elif choice=='EAST':
self.go_east()
player= Character() # create a Character
player.input_loop() # and let the user control it
您可以创建Character
来存储所有必要的数据,而不是使用全局变量,例如是否找到密钥,或者是否已打开抽屉。这样你就不会因变量而混淆你的全局范围。