Friday, January 4, 2019

python - Writing less elif statement

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"


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 are over 100's of animals in the world and I put them in a list, I don't want to create many elif statements.



Is there a way to make it shorter and faster?

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...