简体   繁体   English

为什么此代码返回None值?

[英]Why this code is returning a None value?

I have written this very simple code that should respond to specific user inputs. 我已经编写了这个非常简单的代码,应该可以响应特定的用户输入。 I know I'm doing something wrong, but I don't know what is it. 我知道我做错了,但是我不知道这是什么。 When I type a word or leave blank I get the response "None". 当我输入一个单词或留空时,我得到的响应为“无”。 Thank you in advance :) here is my code: 在此先感谢您:)这是我的代码:

import random
# Sentences we'll respond with if the user greeted us

GREETING_KEYWORDS = ("hello", "hi", "greetings", "sup", "whats up", "You")
GREETING_RESPONSES = ["sup buddy", "Hola", "Halo", "Hi back?"]


def check_for_greeting(sentence):
  """If any of the words in the user's input was a greeting, return a greeting response"""
   if sentence is not None:
       for word in sentence:
           if word.lower() in GREETING_KEYWORDS:
               return random.choice(GREETING_RESPONSES)
           else:
               pass
   elif sentence is None:
       return "You got to say something if you want me to do the same."
   else:
       return "Something is wrong..."


def chat_input():
    x = input("")
    print(check_for_greeting(x))

# Run
print("chat with me...")
chat_input()
for word in sentence:
    if word.lower() in GREETING_KEYWORDS:
        return random.choice(GREETING_RESPONSES)
    else:
        pass

What if none of the words is in GREETING_KEYWORDS ? 如果GREETING_KEYWORDS都没有单词怎么办? What is returned then? 那么返回什么呢? There is no explicit return statement for when that happens, so the answer is: None is returned. 没有发生这种情况的显式return语句,因此答案是: None返回None内容。

You can fix that by returning a default response if the for loop completes without finding a greeting. 如果for循环完成而没有找到问候语,则可以通过返回默认响应来解决此问题。

for word in sentence:
    if word.lower() in GREETING_KEYWORDS:
        return random.choice(GREETING_RESPONSES)
return "default response"

The next problem is that for word in sentence iterates over each letter of the sentence, not each word . 下一个问题是, for word in sentencefor word in sentence会遍历for word in sentence每个字母 ,而不是每个单词 To iterate over words, split the sentence apart: 要遍历单词,请将句子分开:

for word in sentence.split():
    if word.lower() in GREETING_KEYWORDS:
        return random.choice(GREETING_RESPONSES)
return "default response"

Next, I see that you want to detect a two-word greeting: whats up . 接下来,我看到您想检测两个单词的问候:怎么whats up That's not going to work since splitting the sentence into words means you can only check for one-word greetings. 这是行不通的,因为将句子分成单词意味着您只能检查一个单词的问候。 I'd suggest inverting the logic: loop over all the possible greetings and see if they're in the sentence. 我建议反转逻辑:遍历所有可能的问候,看看它们是否在句子中。 (Notice that now you don't want to split the sentence into words.) (请注意,现在你不想分裂成句的话。)

for greeting in GREETING_KEYWORDS:
    if greeting in sentence:
        return random.choice(GREETING_RESPONSES)
return "default response"

Finally, this is a little more advanced, but you could use a generator expression plus any() to simplify the check down to a simple if/else. 最后,它稍微先进一点,但是您可以使用生成器表达式加any()将检查简化为简单的if / else。

if any(greeting in sentence for greeting in GREETING_KEYWORDS):
    return random.choice(GREETING_RESPONSES)
else:
    return "default response"

I like this because it reads more like plain English. 我喜欢它,因为它读起来更像是普通的英语。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM