Colors for Rows

J. Cliff Dyer jcd at sdf.lonestar.org
Tue Apr 29 15:03:23 EDT 2008


On Tue, 2008-04-29 at 13:14 -0500, Victor Subervi wrote:
> On Tue, Apr 29, 2008 at 11:11 AM, D'Arcy J.M. Cain <darcy at druid.net>
> wrote:
>         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.
>  
> Not sure what you mean here. The example runs. It prints out <tr
> bgcolor="#ffffff"> every time.
>  
>         
>         
>         >   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
>  
> Huh? You´re asking for one variable, then giving two! How´s that work?
>  

Not quite.  You're passing one variable to the string formatting
operator, and passing a tuple to the print function.  The implicit
parenthesization is not

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

as I think you are suggesting, but rather it is

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


>         
>         
>         Or, tell us what you are trying to do.
>  
> I think you understand. I want the row color to alternate, every
> fourth row color being the same (or a series of 4)
>  
>         
>         
>         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
>  
> I tried that just for fun. It gave a bg of ´f´. Again, how are you
> incorporating d?

If you add that print line to end of your original code, then you'll get
the z%4-th element of bg, which would be one character, because bg is a
string, but if you "define bg as a list of the four colours" first, as
instructed, you'll get sensible results:

bg = ['#ffffff', '#b2b2b2', '#33FF66', '#000000']
for z in (0,1,2,3,4,5,6,7,8,9):
    print ('<tr bgcolor="%s" % bg[z % 4]), z # optional parens

Or, if you aren't sure how many colors you'll be using, try the more
robust:

bg[z % len(bg)]

> TIA,
> Victor

Cheers,
Cliff





More information about the Python-list mailing list