problem with a css cycler
Peter Otten
__peter__ at web.de
Sun Mar 21 02:00:21 EST 2004
Adriano Varoli Piazza wrote:
> Hello all
> I am trying to build a css cycler in python, to change the css used in a
> website every X number of days, using a list of files: the first X days
> it'd show file1, then file2, then file3, then back to the first one.
>
> The code I came up with is the following:
>
> css = ["file1", "file2", "file3"]
> i = 0
> max_i = 3
remove the above line, you might forget updating max_i someday when you
change the css list - len(css) is less errorprone.
> today = int(raw_input("today's date: "))
> stored_day = 1
days = today - stored_day
i = days % len(css)
> print "Variable today is %s .\n" % today
> print "Variable stored_day is %s .\n" % stored_day
> print "Variable i is %s .\n" % i
> print "Variable css is now %s \n" % css[i]
> ----------
> The problem with this is that it works only for the first X days. How do
> I make it "keep state" for the next iterations?
>
> Thanks
The % "modulo" operator gives you the rest of an integer division, e. g:
>>> for i in range(10):
... print i, "% 3 =", i % 3
...
0 % 3 = 0
1 % 3 = 1
2 % 3 = 2
3 % 3 = 0
4 % 3 = 1
5 % 3 = 2
6 % 3 = 0
7 % 3 = 1
8 % 3 = 2
9 % 3 = 0
>>>
Peter
More information about the Python-list
mailing list