【问题标题】:I'm trying to solve this and the ("display" or "screen") works fine but i can't get the other part to work我正在尝试解决这个问题,(“显示”或“屏幕”)工作正常,但我无法让另一部分工作
【发布时间】:2016-05-23 09:34:20
【问题描述】:

我正在尝试解决这个问题,(“显示”或“屏幕”)工作正常,但我无法让另一部分工作,因为它只是停止了代码。以防万一链接在这里不起作用:

if ("display" or "screen") and ("broken" or "blank" or 'black') in problem: 
    print('So your screen is broken? interesting.')

【问题讨论】:

  • 实际上,如果“工作正常”的意思是“按照你的想法去做”,那么任何部分都不能正常工作。
  • 它就像我输入“我的显示器坏了”一样有效(打印位缩进)
  • 尝试一些不包括“显示”或“损坏”的东西,你会发现程序无法分辨。
  • 它适用于屏幕,这是我试图找出替换的另一位(空白或黑色)
  • 我告诉你,不,它不适用于“屏幕”。进行更多测试,您会发现程序始终以相同的方式运行,因为if ("display" or "screen"): 始终评估为真。

标签: python


【解决方案1】:

您的代码的问题是("display" or "screen") 返回"display"("broken" or "blank" or 'black') 返回"broken",因此您不是在搜索您想要搜索的内容。

试试这个:

if "display" in problem or "screen" in problem:
    if "broken" in problem or "black" in problem or "blank" in problem:
         print('So your screen is broken? interesting.')

【讨论】:

    【解决方案2】:

    如果您想检查一个字符串中是否可能包含任意数量的子字符串,那么您可能希望采用以下方法作为解决您正在处理的问题的可能解决方案的指南:

    def main():
        problem = input('What is your problem? ')
        if in_(problem, 'display', 'screen') and \
                in_(problem, 'broken', 'blank', 'black'):
            print('So your screen is broken? Interesting.')
    
    
    def in_(string, *substrings):
        return any(substring in string for substring in substrings)
    
    
    if __name__ == '__main__':
        main()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-14
      • 1970-01-01
      • 1970-01-01
      • 2019-08-31
      • 1970-01-01
      • 2020-04-22
      • 2015-02-27
      • 1970-01-01
      相关资源
      最近更新 更多