Friday 4 January 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...