Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 15
Simply use `in` operator to check whether <greetings_> contains <user_input> if user_input in greetings_: # code here ...
5th Oct 2022, 12:38 PM
Ipang
+ 11
ice , the reason why the initial code does not give any output, is the not suitable use of *any()* builtin method. in this case it returns *True* since there is at least one valid member in the list *greetings_*. > we are comparing (e.g. input is 'hi'): user_input == any(greetings_) -> 'hi' == True -> False if there would be an *else* clause defined, this would be executed. here is a small code to demonstrate how any() can be used: https://code.sololearn.com/cjkqjShN5gLq/?ref=app
5th Oct 2022, 1:11 PM
Lothar
Lothar - avatar
+ 6
if user_input in greetings_: print(random.choice(response_))
5th Oct 2022, 12:38 PM
SoloProg
SoloProg - avatar
+ 3
if any([user_input == word for word in greetings_]): print(random.choice(response_))
5th Oct 2022, 2:08 PM
Solo
Solo - avatar
+ 2
Let me help. Your code: import random user_input = input() greetings_ = ["hi", "hey", "hello",] response_ = ["howdy", "wassup", "hello"] if user_input == any(greetings_): print(random.choice(response_)) The reason why it didn't output any thing is because the user input did no match anything thing return by the any() function. any() would return : True or False, if and item in the iterable is not empty, eg : any([1,0,3]) == True because one of the items is > 0, any([0,0,0]) == False because all the items are ==0 or empty, The same applies to empty strings and non-empty string. The best way is to correct your code in the if condition check it: https://code.sololearn.com/cNWbgF5v6gtn/?ref=app
7th Oct 2022, 6:51 AM
Emmanuel Odukoya
Emmanuel Odukoya - avatar
+ 1
Use != Operater Instead of == u will get ur output
6th Oct 2022, 6:05 PM
Dipali s
Dipali s - avatar
0
ل
6th Oct 2022, 6:21 PM
MOHAMED ETCHAT
0
Bonjour🙏🏽
6th Oct 2022, 8:24 PM
Savané Mohamed
0
Ok
7th Oct 2022, 5:54 AM
Mallepogu Arun
Mallepogu Arun - avatar
- 1
Use != Operater not == 🤩
6th Oct 2022, 5:47 PM
Dipali s
Dipali s - avatar