[Tutor] Is there an easily or shorter way?

Danny Yoo dyoo at hashcollision.org
Tue Dec 16 01:47:48 CET 2014

> Thank you but actually whatever number I get from either 1 to 28,
> each number represent a property name such as "Reading Railroad",
> "Judy Avenue", "Pacific Gas and Electric", etc., etc.
> For example:
> if x = 1 then print "Mediterranean Avenue"
> if x = 2 then print "Baltic Avenue"

Ah, cool!  Ok, then yes, definitely a list.  There's a sequential-ness
here that we should take advantage of.

   PLACES = ["Is-there-a-place-when-x-is-zero?",
                  "Mediterranean Avenue",
                  "Baltic Avenue",
                  ]    # and so on ...

   print PLACES[x]

so that all the conditioning dissolves into a single list lookup.
There might need to be a few more checks to make sure x is in bounds,
but it's worth it here.

More information about the Tutor mailing list