Colors for Rows

D'Arcy J.M. Cain darcy at druid.net
Tue Apr 29 12:11:05 EDT 2008


On Tue, 29 Apr 2008 09:33:32 -0500
"Victor Subervi" <victorsubervi at gmail.com> wrote:
> why doesn't this work?

First, let me remove some blank lines to reduce scrolling.

> z = 3
> 
> for d in (1,2,3,4,5,6):

I changed id to a sequence so that the example actually runs.  Please
run your examples first and cut and paste them into the message after
you are sure that it runs.

>   z += 1
> 
>   if z % 4 == 0:
>     bg = '#ffffff'
>   elif z % 4 == 1:
>     bg = '#d2d2d2'
>   elif z % 4 == 2:
>     bg = '#F6E5DF'
>   else:
>     bg = '#EAF8D5'
> 
> try:
>   print '<tr bgcolor="%s">\n' % bg
> except:
>   print '<tr>\n'
> 
> It never increments z! Yet, if I print z, it will increment and change the
> bgcolor! Why?!

I am not entirely sure what you are trying to do here.  First, what
error condition are you expecting in your try statement.  Second, don't
you want the print clause, with or without the try/except, in the
loop.  I assume that you want to print a line for each member of your
sequence in alternating colours but this only prints for the last one.
Try this:

z = 3

for d in (1,2,3,4,5,6):
  z += 1

  if z % 4 == 0:
    bg = '#ffffff'
  elif z % 4 == 1:
    bg = '#d2d2d2'
  elif z % 4 == 2:
    bg = '#F6E5DF'
  else:
    bg = '#EAF8D5'

  print '<tr bgcolor="%s">' % bg, d

Or, tell us what you are trying to do.

In fact, you can replace all the tests and the print statement with
this after defining bg as a list of the four colours:

  print '<tr bgcolor="%s">' % bg[z % 4], d

-- 
D'Arcy J.M. Cain <darcy at druid.net>         |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 425 1212     (DoD#0082)    (eNTP)   |  what's for dinner.



More information about the Python-list mailing list