【问题标题】:Trying to return a boolean value from a function尝试从函数返回布尔值
【发布时间】: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


【解决方案1】:

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

  • 每个 python 表达式都可以作为布尔值计算
  • NoneFalse00.0,空字符串、列表、元组和字典为False;大多数其他对象是True
  • 这意味着:你通常可以只写x,而不是x == True,在你的情况下:if incomp1:

如果您正在执行一个函数并且它在没有遇到return 的情况下完成,则将隐式返回None。如果您对其进行布尔比较,它将评估为False

【讨论】:

    【解决方案2】:

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

    您也可以将“elif incomp1 == True:”替换为简单的“else”。

    您也可以考虑在choice1 = raw_input("What is your response? :>") 中添加一个.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)
    

    【讨论】:

      猜你喜欢
      • 2013-03-11
      • 2017-06-07
      • 2011-07-22
      • 1970-01-01
      • 1970-01-01
      • 2019-04-14
      • 1970-01-01
      • 2012-02-03
      • 1970-01-01
      相关资源
      最近更新 更多