Python失败的IF语句/更改变量
我有个问题。如能迅速回复,将不胜感激!如果我的函数没有为我正确更改全局变量,那么我的程序将失败。比如说,他们应该能够去南方拿钥匙。有了那把钥匙,他们可以向东走,打开一个锁着的抽屉。除了他们没有通过if检查,无法打开抽屉 提前谢谢!有问题的代码块应该在下面Python失败的IF语句/更改变量,python,if-statement,Python,If Statement,我有个问题。如能迅速回复,将不胜感激!如果我的函数没有为我正确更改全局变量,那么我的程序将失败。比如说,他们应该能够去南方拿钥匙。有了那把钥匙,他们可以向东走,打开一个锁着的抽屉。除了他们没有通过if检查,无法打开抽屉 提前谢谢!有问题的代码块应该在下面 def south(): print ("You can see a key just lying there on the table! What luck!") choice = raw_input("You better
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()
您确实不想使用全局变量,但如果必须使用,您的问题似乎是您没有在
TAKE
条件中分配Key1=1
,而是根据它是否已经具有该值(Key1==1
)返回True
或False
)。请注意,您需要在返回之前设置它
请注意,如果要执行此操作(不执行),则需要在south()函数的顶部断言global Key
要避免全局变量,请从south
返回Key1
的值,并将其传递给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
不过,您必须自己处理对南部
和东部
的调用逻辑。这可能有些过分,但理想情况下,您将按照以下思路进行操作:
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
不使用全局变量,而是创建一个字符
来存储所有必要的数据,例如是否找到了钥匙,抽屉是否已打开。这样你就不会因为变量而使全局范围混乱。嘿,我只是把它改成==1,因为它给了我一个语法错误。我是个新手;如何使用非“全局”且在程序开始时初始化的变量?返回键1==1,moverooms()
的意义是什么?我几乎可以向你们保证,这行代码并没有做你们认为它做的事情——我对python和编程都是新手;在最终放弃并发布到堆栈之前,我多次尝试修复这一问题,其中一次就是把这一行放在这里的。您没有任何全局变量。你认为哪个变量是全局变量?除了没有任何全局变量外,你没有设置任何变量。(除了选项
)