why isnt it working? | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
0

why isnt it working?

this code is like "Siri". you have to write "siri" as the first word and a quwstuin mark at the end in order for it to work. i added a thing if this combination of words are in the sentence it will give a random num between 1-100 ("how many"). why isnt it working when i write this combination of words? code: import re import random my_String = str(input("ask Siri a question:\n")) my_String = my_String.lower() popQ = my_String[-1:] #pop : peek popS = re.findall(r'\w+', my_String) #the re.findall(what to filter , what string to filter from) function fillters all the punctuation marks and returns clean sentence! randChoice = random.choice(["Yes" , "No" , "Sure" , "Maybe" , "Hell nah"]) if (popQ == '?' and popS[0] == "siri"): if ("how many" in popS): #why isnt it working when i write 'how many'? (it should give me a random number between 0-100...) print (random.randrange(100)) else: print (randChoice) elif (popQ != '?' and popS[0] == "siri"): print ("Was that a question?") elif (popQ == '?' and popS[0] != "siri"): print ("Who are you asking?")

10th Jul 2020, 2:31 PM
Yahel
Yahel - avatar
3 Respuestas
+ 2
if "how many" in popS won't work because popS will be a list of single words. "how" may be immediately before "many" in the list popS, but "how many" in popS will still return False. if "how many" in " ".join(popS) should work fine however.
10th Jul 2020, 2:44 PM
Russ
Russ - avatar
+ 1
the problem i found is the condition to check if "how many" was in popS. seperated and checked for each word induvidually and it seems to work https://code.sololearn.com/c9VPFBXucnsl/?ref=app
10th Jul 2020, 2:50 PM
Slick
Slick - avatar
+ 1
Thanks!
10th Jul 2020, 2:55 PM
Yahel
Yahel - avatar