I am a beginner in Python programming. Recently I decided to build an Audio assistant(basically a chatbot with audio), but I ran into an issue when trying to produce the Outputs. I wrote the code in a way that if what the user says/asks the bot to do, is something which has not been defined to the bot or it does not have any commands on what to do if that specific argument is given then it should give a specific output. The code for it is as below:
# to take input from the user:
command = input("Whatever you want to say: ")
command = command.lower()
cmd = command.split()
# below are the commands to give output after processing the input
if 'hi' in cmd:
print('hey')
elif (('how')and('are'))and('you') in cmd:
print('All good! Wbu?')
elif (('hi')and('hru')) in cmd:
print('Hey! Everyting is fine! Wbu?')
else:
print('sorry, did not understand what you meant!')
The problem with the above code is that, if the user says: (hi, hru?) the programme only says: hey. This is because I was using elif statements in the programme. SO I decided to change all of them to if statements:
if 'hi' in cmd:
print('hey')
if (('how')and('are'))and('you') in cmd:
print('All good! Wbu?')
if (('hi')and('hru')) in cmd:
print('Hey! Everyting is fine! Wbu?')
else:
print('sorry, did not understand what you meant!')
what this does is, it prints the output well, but if any other statement's output is supposed to be given, it gives the statement, but also gives the output for else.
Then I tried to define a function for the outputs, and if it is True, i.e. if what the user says has a specified output then it is supposed to give the output, if not, then the programme is supposed to print the exception.
def commands():
if 'hi' in cmd:
print('hey')
if (('how')and('are'))and('you') in cmd:
print('All good! Wbu?')
if (('hi')and('hru')) in cmd:
print('Hey! Everyting is fine! Wbu?')
if commands()==True:
commands()
else:
print('sorry, did not understand what you meant!')
This too as the first, prints the statement as well as the exception. How do I solve this ?
Aucun commentaire:
Enregistrer un commentaire