我试图编程一个简单的基于文本的游戏。为了做到这一点,我让用户去三个不同的房间完成一项任务并收到一份。为了做到这一点,我将一个值设置为False
,然后,在完成房间之后,我有一个return
语句,用于将布尔值更改为True
。当所有三个room函数都返回一个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布尔表达式:
None
,False
,0
,0.0
,空字符串、列表、元组和字典是False
;其他大多数对象是True
x
,而不是x == True
,在您的例子中:if incomp1:
如果您正在执行一个函数,并且它没有命中
return
,那么None
将被隐式返回。如果对它进行布尔比较,它的值将为False
。在您需要将“return incomp1==True”替换为“return True”。然后像这样调用door1函数“incomp1=door1(incomp1)”。 这将把incomp1的值更改为函数“True”返回的值。在
也可以用简单的“else”替换“elif incomp1==True:”。在
您还可以考虑将.lower()添加到choice1=raw_input(“您的响应是什么?:>;“)。这样一来,如果玩家在输入中使用一个大写字母,它仍然可以按需要工作。在
相关问题 更多 >
编程相关推荐