dimanche 13 décembre 2020

Fast way to read possibilities of user input in python

So say I have to read what a user says, and my program does what they say. Something like this:

userinstructions = input('What action would you like me to do?')

if userinstructions == 'walk':
    walk()

elif userinstructions == 'sleep':
    sleep()

elif userinstructions == 'eat':
    eat()

elif userinstructions == 'talk':
    talk()

now, let's say that there are hundreds of possibilities just like in real life. I wouldn't want to do if statements for possibly hundreds of statements. Is there a way to make this faster and have less code? Like maybe a loop or something. I have played around with it for a bit, but I can't come up with anything.

Aucun commentaire:

Enregistrer un commentaire