'How to compare the input statement with the mood list like if I entered I am fine in input?

mood = ['Fine', 'bad', 'good', 'sad', 'angry']
a = input("how are You  ").split()
print(a)


Solution 1:[1]

You could loop over the list and compare each element:

moods = ['fine', 'bad', 'good', 'sad', 'angry']
mymood = input("how are You  ").strip()

for mood in moods:
    if mymood == mood:
        break
else:
    raise ValueError("I can't tell your mood")

But there are better ways in Python. You can use the containment operator, in.

if mymood not in moods:
    raise ValueError("I can't tell your mood")

Even better if moods was a set.


moods = {'fine', 'bad', 'good', 'sad', 'angry'}

# The test looks exactly the same, but will be faster for large sets.
if mymood not in moods:
    raise ValueError("I can't tell your mood")

After that, the mymood variable is known to be one of the moods.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Keith