I can't use the dictionary function well with pod.

Asked 2 years ago, Updated 2 years ago, 18 views

"The code below for making bot is not recognized except ""Hello"", and it does not respond well."
The input character is utf8 and python is version 3.6.

bot_dict={
    "Hello" : "Konnichiha",
    'Thank you': 'Dowitashimashite',
    "Goodbye": 'Goodbye',
    }

while True:
    command=input('pybot>')
    response='"
    for key in bot_dict:
        if key in command:
            response=bot_dict [ key ]
            break

        if not response:
            response= 'What do you say, dolphin, wakaranai'
        print(response)

        If 'Goodbye' in command:
            break

src=

python

2022-09-30 14:14

1 Answers

Indentation is causing the response to be displayed

 if not response:
    response = 'What do you say, Wakaranai'
print(response)

If 'Goodbye' in command:
    break

The reason is that you are in the for loop.

If you follow the code a little bit, when you type 'Hello'

for key in bot_dict:
    if key in command:
        response=bot_dict [ key ]
      break

It matches the conditions of the if statement, but break skips the response part because it leaves the loop, and the response does not appear.

Also, if you enter 'thank you', you will not be able to break the loop compared to 'hello' at first, so you are running the answer part.


2022-09-30 14:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.