I am trying to write code for a chatbot (LSAR) where I am manually putting in commands and answers. I have multiple commands set, and I am trying to figure out how to set an error message.
In the last line there, I put all the commands in curly brackets, separated by commas (I tried with square brackets too if it matters.) This was supposed to make it so that when you put in an incorrect command, it gives you the error message and lets you try again. That seemed to work at first, but now whenever I put in something that isn't a command, it will make all following commands show the error message as well. E.G. I type in "asdf", LSAR sends its error message, I type in "CYN", it sends the error message again. This is only my second or third time coding in python so I'm really not sure how to figure out what the issue here is; any sort of help is greatly appreciated. Here is my code:
while var == "How are you?": var = input("\nim s0 g00d!! im just chilllin as usuallll ykn0w >_<\n\n")
while var == "What's your name?": var = input("\nim LSAR! nice t0 meet y0u friend :3\n\n")
while var == "CYN": var = input ("\n[CALLBACK PING]\n\n")
while var == "Who are you?": var = input("\nIt seems you have asked about LS's chat client auto-responder. This is an application designed to simulate LS's otherwise inimitable rad typing style, tone, cadence, personality, and substance of retort while he is unavailable. The algorithms are guaranteed to be 93 percent indistinguishable from LS's native neurological responses, based on some statistical analysis I basically just pulled out of my ass right now.\n\n")
while var == "What are you?": var = input("\nim n0t a what. be nice!! X(\n\n")
while var != {"How are you?", "CYN", "Who are you?", "What are you?", "What's your name?"}: var = input("ummmm wat. s0rry, idk what y0ure talllking ab0ut! X(\ntry an0ther c0mmand plllease, 0ne i understand this time ^_^\n\n"```
var != {"How are you?", "CYN", "Who are you?", "What are you?", "What's your name?"}
actually checks if var
is a set of all those strings rather than checking if var
is equal to any one of them.
Another issue is that you only check for commands in the order that they've been listed in. For example, trying "CYN" after "Who are you?" would give no response since you're already past that while statement.
Store your call and response as a dictionary, use a dict.get()
to grab your response or return an error response if the command is not recognized, and use a single while loop:
commands = {
"How are you?": "\nim s0 g00d!! im just chilllin as usuallll ykn0w >_<\n\n",
"What's your name?": "\nim LSAR! nice t0 meet y0u friend :3\n\n",
"CYN": "\n[CALLBACK PING]\n\n",
"Who are you?": "\nIt seems you have asked about LS's chat client auto-responder. This is an application designed to simulate LS's otherwise inimitable rad typing style, tone, cadence, personality, and substance of retort while he is unavailable. The algorithms are guaranteed to be 93 percent indistinguishable from LS's native neurological responses, based on some statistical analysis I basically just pulled out of my ass right now.\n\n",
"What are you?": "\nim n0t a what. be nice!! X(\n\n"
}
error_message = "ummmm wat. s0rry, idk what y0ure talllking ab0ut! X(\ntry an0ther c0mmand plllease, 0ne i understand this time ^_^\n\n"
var = input("Ask a question or quit")
while var.lower() != "quit":
response = commands.get(var, error_message)
var = input(response)