[英]Python failing IF STATEMENTS/ Changing Variables
I'm having an issue. 我有一个问题。 Speedy responses would be greatly appreciated!
迅速的响应将不胜感激! My program is failing IF conditions becuase my functions are not changing the global variable properly for me.
如果我的程序不能正常运行,因为我的函数没有为我正确更改全局变量。 They're supposed to, for example, be able to go south and take a key.
例如,他们应该能够向南走,并取得钥匙。 With that key they can go East and open a locked drawer.
有了那个钥匙,他们就可以向东走,打开一个上锁的抽屉。 Except... they fail the if check to be able to open the drawer.
除了...他们没有通过if检查能够打开抽屉。
Thanks in advance! 提前致谢! The code blocks in question should be below!
有问题的代码块应该在下面!
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()
You really don't want to use global variables, but if you must, your problem seems to be that you're not assigning Key1 = 1
in your TAKE
conditional, but returning True
or False
according to whether or not it already has that value ( Key1==1
). 您确实不想使用全局变量,但是如果必须的话,您的问题似乎是您没有在
TAKE
条件中分配Key1 = 1
,而是根据是否已经具有该值返回True
或False
( Key1==1
)。 Note that you need to set it before the return
. 请注意,您需要在
return
之前进行设置。
Note that if you want to do this (you don't), you'll need to assert global Key
at the top of your south()
function. 请注意,如果要这样做(不需要),则需要在
south()
函数的顶部声明global Key
。
To avoid global variables, return a value for Key1
from south
and pass it to east
: 为了避免全局变量,请从
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
You'll have to handle the logic of the calls to south
and east
yourself, though. 不过,您必须自己处理打向
south
和east
的电话的逻辑。
This might be overkill, but ideally you'll do something along these lines: 这可能是矫kill过正,但理想情况下,您将按照以下方式进行操作:
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
Instead of using global variables, you create a Character
to store all necessary data, like whether the key was found, or whether the drawer has been opened. 无需使用全局变量,而是创建一个
Character
来存储所有必需的数据,例如是否找到了键或是否已打开抽屉。 That way you won't clutter your global scope with variables. 这样,您就不会使变量的全局范围混乱。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.