【问题标题】:Efficient approach for multiple 'or' statements in an conditional statement条件语句中多个“或”语句的有效方法
【发布时间】:2015-11-16 01:22:31
【问题描述】:

学习 Python 才几个月,所以请耐心等待......

创建一个非常简单的游戏作为 Python 教程的一部分,其中基本上用户从一个班级跳到另一个班级,所有这些都需要用户输入并打印出各种语句。

例如,给定以下类:

class ChipCar(Scene):

    def enter(self):
        print "What's up? Get in the Bran Muffin car!"

        action = raw_input(">  ")

        if action == "shut up chip":
            print "Screw you!"
            print action
            return next_scene('Your_death')
            #return 'Death' 
        elif action == "hi chip":
            print "What's up loser?!?! Let's go to O&A..."
            return next_scene('Chip_in_studio')
        else:
            print "what's wrong with you? Let's go to my mother's house, I think Lamar's there..."
            return 'Chip_mom_house'

假设我希望能够检查第一个 if 语句的多个正确选项,例如,除了 "shut up chip" 之外还有 "go away dude""screw you""Java sucks" — 最好的方法是什么?

【问题讨论】:

  • 哇,有很多选择......有点违背 Python 哲学,嗯?关于“应该有一种——最好只有一种——明显的方式来做到这一点。”

标签: python python-2.7 conditional-statements


【解决方案1】:

如果您的所有测试都是为了相等,您可以在集合上使用in 运算符,它将检查您正在测试的值是否存在。我建议使用set,因为它的成员资格测试非常快,但是使用listtuple 可能会在Python 2 中获得更好的性能(我认为只有在Python 3 的最新版本中,常量集文字才是存储为常量,而不是在每次函数运行时重新创建):

if action in {"shut up chip", "go away dude", "screw you", "Java sucks"}:
    # ...

如果您有不相关的非等式测试,则可以使用anyall 函数将它们链接起来,就像使用orand 一样。例如,此代码将测试与上述相同的字符串,但允许任意大小写以及在我们正在检查的位旁边包含额外的文本(它进行子字符串搜索):

if any(phrase in action.lower() for phrase in ("shut up chip", "go away dude",
                                               "screw you", "java sucks")):
    # ...

这是在 any 调用中使用生成器表达式,这是 Python 中非常常见的习语(但可能比您所学的要高级一些)。

【讨论】:

  • 感谢这个。关于您提供的酷生成器表达式选项:如果用户输入同时包含当前元组中的一个短语以及为elif 语句设置的短语,例如"shut up chip hi chip",会发生什么情况
  • 因为您使用的是ifelif,所以只有第一个可以工作(永远不会检查elif 的条件)。如果您希望能够检测单个输入的多个不同匹配项,您可能需要使用更复杂的逻辑。
【解决方案2】:

你可以这样做:

def enter(self):
    print "What's up? Get in the Bran Muffin car!"

    action = raw_input(">  ")

    if(action == "shut up chip" or action == "go away dude" or action == "screw you"):
        print "Screw you!"
        print action
        return next_scene('Your_death')
        #return 'Death' 
    elif action == "hi chip":
        print "What's up loser?!?! Let's go to O&A..."
        return next_scene('Chip_in_studio')
    else:
        print "what's wrong with you? Let's go to my mother's house, I think Lamar's there..."
        return 'Chip_mom_house'

我们在这个条件语句中使用or 的方式是在布尔上下文中从左到右计算值,就像and 一样。如果任何值为真,or 会立即返回该值。您可以将其视为将所有不同的用户响应选项包装在括号内,其中我们正在评估action 的每个实例是否具有您的用户输入值之一。

【讨论】:

    【解决方案3】:

    您可以在字符串元组上使用in 运算符。如果有匹配则返回True

    class ChipCar(Scene):
        def enter(self):
            print "What's up? Get in the Bran Muffin car!"
    
            action = raw_input(">  ")
    
            if action in ("shut up chip", "go away dude", "screw you"):
                print "Screw you!"
                print action
                return next_scene('Your_death')
                #return 'Death' 
            elif action == "hi chip":
                print "What's up loser?!?! Let's go to O&A..."
                return next_scene('Chip_in_studio')
            else:
                print "what's wrong with you? Let's go to my mother's house, I think Lamar's there..."
                return 'Chip_mom_house'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-21
      • 1970-01-01
      相关资源
      最近更新 更多