I am trying to write a very basic code that can take the input given by a user and sift through a list of positive responses to see if the user's response is part of that list. The code below iterates for each item in the list but I do not want that, instead I want it to check all items in the list and print out something only if the input is in the list. How can I do that?
positive_responses = ['Good', 'Fine', 'Okay', 'Great']
user_response = input('How are you today? - ')
for each in positive_responses:
if user_response == each:
print('Glad to hear that')
elif user_response != each:
print('What can we do to help?')
If I understand you correctly you can use in
operator:
positive_responses = ["Good", "Fine", "Okay", "Great"]
user_response = input("How are you today? - ")
if user_response in positive_responses:
print("Glad to hear that")
else:
print("What can we do to help?")
Prints (for example):
How are you today? - Great
Glad to hear that
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.