I'm developing a chatbot project for college, and in the following code block, the first if is always going as a true value, no matter what. I really need help and don't know what to do, cause this project is due on monday.
def registeredClient():
print('Olá, bem-vindo a WE-RJ Telecom!')
userInputString = str(input('O que você precisa?\nCaso queira contratar ou trocar de plano escreva "Quero contratar" ou "Quero trocar de plano".\nCaso esteja com problemas de conexão, escreva "suporte".\nCaso queira seu boleto, digite "boleto":\n'))
userInputString = userInputString.lower()
if 'contratar' or 'trocar plano' or 'aumentar velocidade' or 'mudar plano' or 'velocidade' or 'plano' in userInputString:
newPlanOption()
elif 'suporte' or 'lenta' or 'internet lenta' or 'internet esta lenta' or 'problema' or 'velocidade' in userInputString:
supportOption()
elif 'boleto' or 'segunda via' or '2a via' or 'fatura' in userInputString:
billingOption()
else:
print('Não foi posível entender a sua mensagem, seu atendimento será encerrado.')
return False
2 Answers 2
I updated the conditions. In your case your conditions were checking if the strings themselves were truthly which is why your first case would result in true.
def registeredClient():
print('Olá, bem-vindo a WE-RJ Telecom!')
userInputString = str(input('O que você precisa?\nCaso queira contratar ou trocar de plano escreva "Quero contratar" ou "Quero trocar de plano".\nCaso esteja com problemas de conexão, escreva "suporte".\nCaso queira seu boleto, digite "boleto":\n'))
userInputString = userInputString.lower()
if any(x in userInputString for x in ['contratar', 'trocar plano' , 'aumentar velocidade' , 'mudar plano' , 'velocidade' , 'plano']):
print("Case A")
elif any(x in userInputString for x in ['suporte', 'lenta' , 'internet lenta' , 'internet esta lenta' , 'problema' , 'velocidade']):
print("Case B")
elif any(x in userInputString for x in ['boleto' , 'segunda via' , '2a via' , 'fatura']):
print("Case C")
else:
print('Não foi posível entender a sua mensagem, seu atendimento será encerrado.')
return False
registeredClient();
Comments
The first if block is understood by python as the following if block :
(if 'contratar') or ('trocar plano') or ('aumentar velocidade') or ('mudar plano') or ('velocidade') or ('plano' in userInputString):
which is always True as the strings are not vacant and thus truthy type.
What you need is this as the first if block :
if any(i in userInputString for i in ['contratar', 'trocar plano', 'aumentar velocidade', 'mudar plano', 'velocidade', 'plano']):
Similarly you need to change your elif statements too.
Try this :
def registeredClient():
print('Olá, bem-vindo a WE-RJ Telecom!')
userInputString = str(input('O que você precisa?\nCaso queira contratar ou trocar de plano escreva "Quero contratar" ou "Quero trocar de plano".\nCaso esteja com problemas de conexão, escreva "suporte".\nCaso queira seu boleto, digite "boleto":\n'))
userInputString = userInputString.lower()
checkString = lambda l: any(i in userInputString for i in l)
if checkString(['contratar', 'trocar plano', 'aumentar velocidade', 'mudar plano', 'velocidade', 'plano']):
newPlanOption()
elif checkString(['suporte', 'lenta', 'internet lenta', 'internet esta lenta', 'problema', 'velocidade']):
supportOption()
elif checkString(['boleto', 'segunda via', '2a via', 'fatura']):
billingOption()
else:
print('Não foi posível entender a sua mensagem, seu atendimento será encerrado.')
return False
Comments
Explore related questions
See similar questions with these tags.
contrataralways has the value True, so it will always take theif. You surely wantif input_string in ('contratar, 'trocar plano', 'aumentar velocidade' ...):.