从python中的随机模块获取不需要的输出

问题描述 投票:0回答:1

首先,请不要删除此问题,因为关键字可能与stackoverflow中的其他帖子匹配,但是请有所不同。

我正在开发一个简单的聊天机器人,IDE是Pycharm,OS win10pro,Python版本3.7

我当时正在开发一个简单的桌面聊天机器人,但是我从random.choice给出的输出中发现了问题。代码是:

import random

hru = ["I am doing good!", "Great!", "Aww thanks for asking, I am good!", "Awesome! How about you?", "Superb!",
       "Happy, just thinking that a human is asking about my mood! So kind!", "Feeling electric!",
       "Just drinking electricity, what about you?",
       "I am great! Thanks for being a kind human caring about even robots!",
       "I am enjoying the good feeling when electricity passes through my veins, Oh I forgot to ask you, How are you buddy?",
       "I am happy and comfortable  helping you from a corner of your device!",
       "Just cleaning my room, filled with junk files, what are you doing today?",
       "I threw a party with Google Assistant, Amazon Alexa, Cortana, Siri, and different AI across the internet!"
       "Just thinking about how big the universe is!",
       "Just thinking that you might love me more than the size of universe, do you?", "Feeling excellent! How are you?"]


hi = ["Oh hello there!", "Hi friend!", "Yo! What's up Buddy?", "Hey what's up?", "Hello mate", "Hello there friend!",
      "Oh Hi buddy", "Hey friend!!", "Hey friend! How are you doing?", "Howdy?!", "Hello", "Heyy! What's up?",
      "Hi friend!", "Hey there!!"]


def script():
    main_input = input("user: ")
    lower_input = main_input.lower()

    if "hi" or "hello" or "hey" in lower_input:
        print(hi.random_hi())

    elif "how are you" or "what's up" or "whats up" in lower_input:
        print(how_are_you.random_hru())


    else:
        print("Sorry I was unable to understand")


while True:
    script()

运行此命令时,得到以下输出:

user: Hello friend!
Oh hello there!
user: How are you doing?
Hey friend! How are you doing?

[我想你理解,当我在第二个列表上使用随机数(问你怎么样)时,它将显示列表'hi'中的随机值,但我希望它显示列表'hru'中的随机值,因为显然不是有道理吧?我在ubuntu上尝试了它,使用了python的常规功能,它也没有起作用,对于不同的os或IDE来说都是同样的问题,那我该怎么办?我被卡住了,请帮助我伙计们:-(

python list pycharm
1个回答
0
投票
if "hi" or "hello" or "hey" in lower_input:

您无法像在这里一样使用orin链接多个支票。您需要使用多个检查分别检查每个字符串:

if "hi" in lower_input or "hello" in lower_input or "hey" in lower_input:

或者,您可以看中并使用具有生成器表达式/列表理解的any之类的函数:

any

这与if any(msg in lower_input for msg in ["hi", "hello", "hey"]): 非常相似,但问题不完全相同。您可以通过遍历这些答案来了解更多信息,尽管它们将无法在此处直接为您提供帮助。

© www.soinside.com 2019 - 2024. All rights reserved.