【发布时间】:2015-10-05 08:12:00
【问题描述】:
我正在尝试编写一个简单的基于文本的游戏。为了做到这一点,我让用户去三个不同的房间完成一项任务并收到一块。为此,我将值设置为False,然后,在房间完成后,我有一个return 语句用于将布尔值更改为True。当所有三个房间函数都返回 True 时,我将其设置为打开一个推进游戏的函数。然而,众所周知,返回不会发生,因此布尔值保持不变。
下面是一个门函数的例子:
def door1(incomp1):
if incomp1 == False:
print 'You enter door 1 and find a hideous internet troll.'
print "The troll says, 'LOOK AT THIS LOSER'"
options = ["YOU'RE THE LOSER", "I don't care." , "YOUR MOM IS FAT"]
options2 = [1,2,3]
for x in options:
print "\t :> %s" % x
choice1 = raw_input("What is your response? :>")
if ('loser' in choice1) or ('fat' in choice1):
print "You are sucked into the troll's behaviour and are driven insane"
print "After three days of insanity, you starve to death"
dead()
elif 'care' in choice1:
print 'The troll cannot feed off of your anger.'
print 'The troll starves and you recover one piece of the lever from his stomach.'
change()
entrywayopen()
return incomp1 == True
else:
unknown()
change()
door1(incomp1)
elif incomp1 == True:
print 'You already recovered the piece from room 1.'
entrywayopen()
所以,当调用函数时,我的 incomp1 的值已经是 False。用户必须得到正确的答案,在本例中为elif 语句。最后,我把它交给return incomp1 == True。然而,价值保持不变。我在这个房间策略中的最后一步是为语句if door1(incomp1) and door2(incomp2) and door3(incomp3): 返回一个值True。是什么导致不返回布尔值更改?
【问题讨论】:
-
你试过在这个上面运行调试器吗?
-
尝试在调用函数之前打印您传递给该函数的变量的值。房间建成后如何存储这些值?
标签: python if-statement return boolean