How should I improve this?-Python
h
LuckyOreos (200)

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?”

You are viewing a single comment. View All
Answered by EpicGamer007 (1626) [earned 5 cycles]
View Answer
brandosha (20)

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.