How should I improve this?-Python
This is basically a chat bot program which lets you talk to Star Wars Characters!!! It’s in progress and I might submit it into the AI competition!!! But since I’m learning Python at a pretty slow pace, mainly because of COVID. I need help. My main question about this program is “When you are chatting with the Chat Bots in this program, How do I program it so that you can ask more than 1 question and don’t have to like press Run again to ask another one?”
Thanks epicgamer007 and everyone
You can clean up your code a lot by using dictionaries: https://docs.python.org/3/tutorial/datastructures.html#dictionaries
You can do something like this
whatever_bot = {
'input1': 'response1',
'input2': 'response2'
}
while True:
message = input(': ').lower()
default_response = 'I don\'t understand' # Will show this if unknown input
response = whatever_bot.get(message, default_response)
print('Bot: ' + response)
Notice how I used .get()
instead of indexing. This enables a default response instead of throwing an error.
Also notice how it is in a while True
loop so it will repeatedly ask for input.
@brandosha thanks
don't use so many ifs
. Try using elifs
too.
@RYANTADIPARTHI thank u I’ll try that I’m also doing this assignment for school so I want it to be perfect or near perfect. And I’m going to try that
@DarthVader29 no problem.
use a loop.
https://www.tutorialspoint.com/python/python_loops.htm
@EpicGamer007 ok thanks