Ask coding questions

← Back to all posts
How should I improve this?-Python
LuckyOreos (186)

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

Answered by EpicGamer007 (1464) [earned 5 cycles]
View Answer
Comments
hotnewtop
LuckyOreos (186)

Thanks epicgamer007 and everyone

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.

RYANTADIPARTHI (5693)

don't use so many ifs. Try using elifs too.

LuckyOreos (186)

@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