mercredi 20 juillet 2016

writing less elif statement

Hi y'all i need help with this.

a = ["cat","dog","fish","hamster"]

user = raw_input("choose your fav pet ")

if user == a[0]:

print a[0]

elif user == a[1]:

print a[1]

elif user == a[2]:

print a[2]

elif user == a[3]:

print a[3]

else:

print "sorry, the aninimal you type does not exist"

OK, what i want to do is a testing mobile app so i use animal as testing. the program did work but the problem is that there is over 100's of animals in the world and i put it in the list, i don't want to create many "elif" statement. So is there a way to make it shorter and faster. please do explain the answer step by step so that i can learn. Thank you :)

Aucun commentaire:

Enregistrer un commentaire