+ 15
Simply use `in` operator to check whether <greetings_> contains <user_input>
if user_input in greetings_:
# code here ...
+ 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
+ 6
if user_input in greetings_:
print(random.choice(response_))
+ 3
if any([user_input == word for word in greetings_]):
print(random.choice(response_))
+ 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
+ 1
Use != Operater Instead of == u will get ur output
0
Ù
0
Bonjourđđœ
0
Ok
- 1
Use != Operater not == đ€©