MLCLOUD MLCLOUD - 3 months ago 8
Python Question

How to allow many responses for if statement and replies?

I am making a sort of Siri program. But I'm not sure how to allow various responses, and various replies at random.

Does it involve dictionaries or a simpler alternative, or a random function?

if greeting == "Hello", "Hi", "Hey":
print("Hello", "Hi", "Hey")
else:
print("I'm not sure what you said there")

Answer

To pick a random response you can use random.choice, which selects a random element from a list:

import random

if greeting in ["Hello", "Hi", "Hey"]:
    response = random.choice(["Hello", "Hi", "Hey"])
    print(response)
else:
    print("I'm not sure what you said there")