Skip navigation

I like to have as many resources available when I need help.  Who doesn’t?  So, besides Google (which needs a certain skill to get the right answer to your specific problems typically), I also make great use from IRC.  These are the channels I use on freenode.net (YOu can get to them either by http://webchat.freenode.net or download a client like XChat)

#python (You need to have a registered nickname as well ask for a ‘cloak’ in the #freenode channel, otherwise you don’t get in.  It’s all free, but just need to ask.)

#python-apac (This is a new one for me as of today.  It says it’s for the Asia Pacific crowd but there’s only 2 people in there right now and I’m one of them.  Was hoping to find some english speaking Python guys in my own timezone to chat with.)

#python-forum (This is the official channel for python-forum.com, not a lot of people there but also not many who are currently active for my time of the day too.  I suspect it might be a mostly ‘western’ timezone active channel)

#dragondon (This is my own personal channel.  A few geeks hang out there.)

I just registered on python-forum.org.  I wanted a place that I could post up questions.  Seems a cool place so far.  They have some cool tips and I thought I would share one of them that I found.

****

Avoiding massive elif statements
the first example shows elif statements as you would learn them in any tutorial, the second however shows the same thing done but with no elif statements. It does the same thing but reduces the code and makes it more debugable in the future.

choice = input(‘enter a number’)
if choice == ‘0’:
print(‘you chose zero’)
elif choice == ‘1’:
print(‘you chose one’)
elif choice == ‘2’:
print(‘you chose two’)
elif choice == ‘3’:
print(‘you chose three’)
else:
print(‘out of range/invalid’)

user_choice = {‘0′:’zero’,’1′:’one’,’2′:’two’,’3′:’three’}
choice = input(‘enter a number’)
try:
print(‘you chose {}’.format(user_choice[choice]))
except KeyError:
print(‘out of range/invalid’)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: