【问题标题】:While loop is not iterating over the correct solution in array虽然循环没有迭代数组中的正确解决方案
【发布时间】:2016-10-24 17:00:47
【问题描述】:

我的目标:我试图让用户输入他们自己的查询以获取故障排除系统。如果用户的输入具有在“关键字”数组中找到的关键字,则从“答案”数组中的相同索引给出解决方案。

问题:没有语法错误,而是逻辑错误。对于 'keywords' 数组中的第一个和第二个索引,如果输入了这个关键字,则给出正确的解决方案。但是,对于 'keywords' 数组中的第三个和第四个索引,它会从 'answers' 数组中的不同索引输出错误的解决方案。

我的代码:

answers = ['dry it out','replace screen','delete any apps that are not needed','restart it']
keywords = ['wet','cracked','download','unresponsive']
i = 0
while i <= 5:
    user_query = str(input('What\'s the problem?\n>> ')).lower()
    for keyword in keywords:
        while keyword[i] not in user_query:
            i = i + 1
        if keyword[i] in user_query:
            print(answers[i])
            i = 10
            break
        if i >= 5:
            print('contact the supplier')
            break

【问题讨论】:

    标签: python arrays indexing while-loop counter


    【解决方案1】:

    您必须记住,在for keyword in keywords: 中,关键字是一个字符串,通过i 对其进行索引是提取单个字母。相反,你想做这样的事情:

    for keyword in keywords:
        if keyword in user_query:
            # Handle things here
    

    for i in range(len(keywords)):
        if keyword[i] in user_query:
             # Handle things here
    

    第二种方法将允许您引用answers 数组中的相应条目,所以这是我推荐的。

    您仍然需要清理并确保用户在代码中的正确位置输入查询。我的猜测是您想要的代码(尽管您应该验证)是:

    answers = ['dry it out','replace screen','delete any apps that are not needed','restart it']
    keywords = ['wet','cracked','download','unresponsive']
    user_query = str(input('What\'s the problem?\n>> ')).lower()
    
    
    for i in range(len(keywords)):
        if keyword[i] in user_query:
            print(answers[i])
            break
    else:
        print('contact the supplier')
    

    此代码使用附加到for 循环的else 块。要了解发生了什么,我建议您阅读它们here

    【讨论】:

      【解决方案2】:
      answers = ['dry it out','replace screen','delete any apps that are not needed','restart it']
      keywords = ['wet','cracked','download','unresponsive']
      
      query = input('What\'s the problem?\n>> ').lower()
      
      try:
          print(answers[keywords.index(query)])
      except ValueError:
          print("Contact the supplier.")
      

      这是另一个使用内置索引函数的列表而不需要 for 循环的选项。

      【讨论】:

        【解决方案3】:

        我认为这可能会更好一些:

        answers = ['dry it out','replace screen','delete any apps that are not needed','restart it']
        keywords = ['wet','cracked','download','unresponsive']
        responses = {k:v for k,v in zip(keywords,answers)}
        
        def getAnswer(query, solutions):
            for keyword in solutions:
                if keyword in query:
                    return solutions[keyword]
            return "Contact the supplier"
        
        user_query = str(input('What\'s the problem?\n>> ')).lower()
        print(getAnswer(user_query,responses))
        

        示例输出:

        What's the problem?
        >> screen cracked
        replace screen
        

        【讨论】:

        • 这可以很容易地扩展为返回解决方案列表,以防多个关键字匹配,如果这是您想要做的事情
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-13
        • 2020-06-08
        • 2011-01-19
        • 2023-03-28
        • 2015-03-02
        • 1970-01-01
        相关资源
        最近更新 更多