如何使Python bot回复列表中的相同数字

发布于 2025-02-11 20:41:37 字数 118 浏览 1 评论 0原文

例如,我做了这样的代码: etch = [“你好”,“嗨”] 关键字= [“ hi”,“ Hello”] 我该如何制作,以便发送相同的列表号码?例如,Hello在问候列表上是0,所以我会发送HI,因为关键字列表上的0是0?

If for example I did a code like this:
Greet = [“hello”, “hi”]
Keyword = [“hi”, “hello”]
How do I make it so I send the same list number? For example hello would be 0 on the Greet list so I would send hi as it is 0 on the keyword list?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍暮颜 2025-02-18 20:41:37

这就是您的想法:

greetings = ["hi", "hello", "yo"]
keywords = ["Hi!", "Hello!", "Yo!"]

while True:

    user = input("what would you like to say?: ")

    if user == "bye":
        break

    try:
        print(keywords[greetings.index(user)])
    except ValueError:
        print('I don\'t know how to reply.')

更多的Pythonic是:

answers = {"hi": "Hi!",
           "hello": "Hello!",
           "yo": "Yo!"}

while True:
    
    user = input("what would you like to say?: ")
    
    if user == "bye":
        break
    
    try:
        print(answers[user])
    except KeyError:
        print('I don\'t know how to reply')

或:

from collections import defaultdict

answers = defaultdict(lambda:'I don\'t know how to reply.')
answers.update({"hi": "Hi!",
                "hello": "Hello!",
                "yo": "Yo!"})

while True:
    user = input("what would you like to say?: ")
    if user == "bye":
        break
    print(answers[user])

This would be what you have in mind:

greetings = ["hi", "hello", "yo"]
keywords = ["Hi!", "Hello!", "Yo!"]

while True:

    user = input("what would you like to say?: ")

    if user == "bye":
        break

    try:
        print(keywords[greetings.index(user)])
    except ValueError:
        print('I don\'t know how to reply.')

More pythonic would be:

answers = {"hi": "Hi!",
           "hello": "Hello!",
           "yo": "Yo!"}

while True:
    
    user = input("what would you like to say?: ")
    
    if user == "bye":
        break
    
    try:
        print(answers[user])
    except KeyError:
        print('I don\'t know how to reply')

or:

from collections import defaultdict

answers = defaultdict(lambda:'I don\'t know how to reply.')
answers.update({"hi": "Hi!",
                "hello": "Hello!",
                "yo": "Yo!"})

while True:
    user = input("what would you like to say?: ")
    if user == "bye":
        break
    print(answers[user])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文