试图从函数返回一个布尔值

时间:2015-07-16 03:11:07

标签: python if-statement return boolean

我正在尝试编写一个简单的基于文本的游戏。为了做到这一点,我让用户去三个单独的房间完成任务并收到一块。为了做到这一点,我将值设置为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。然而,价值保持不变。我在此会议室策略中的最后一步是为语句True返回值if door1(incomp1) and door2(incomp2) and door3(incomp3):。是什么导致值的布尔值更改不返回?

2 个答案:

答案 0 :(得分:0)

关于Python布尔表达式的一点:

  • 每个python表达式都可以作为布尔值
  • 进行评估
  • public static void ProceedButtonClick() { Driver.WaitForAndIsEnabledFind(ProceedButtonElementBy).Click(); Driver.WaitFor(LoadedPageFinishedIdentifierBy); } NoneFalse0,空字符串,列表,元组和字典为0.0;大多数其他对象是False
  • 这意味着:代替True,您通常只需撰写x == True,在您的情况下:x

如果您正在执行某个功能并且在没有点击if incomp1:的情况下完成,则会隐式返回return。如果对它进行布尔比较,它将评估为None

答案 1 :(得分:0)

您需要将“return incomp1 == True”替换为“return True”。然后调用door1函数,如“incomp1 = door1(incomp1)”。 这会将incomp1的值更改为函数“True”返回的值。

您还可以使用简单的“else”替换“elif incomp1 == True:”。

您也可以考虑在choice1 = raw_input(“您的回复是什么?:>”)中添加.lower()。如果玩家在输入中使用大写字母,它仍将按照需要运行。

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? :>")
        choice1 = raw_input("What is your response? :>").lower()
        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 True
        else:
            unknown()
            change()
            door1(incomp1)
    #elif incomp1 == True:
    else:
        print 'You already recovered the piece from room 1.'
        entrywayopen()

incomp1 = door1(incomp1)