jem00

is it possible to create a looping list?

``````import time

# a day value of None = >
def dayOfWeek(DayNum = None):
# match day order to Python return values
days = ['Monday','Tuesday',
'Wednesday','Thursday',
'Friday','Saturday','Sunday']

theTime = time.localtime(time.time())
# Check for default value
if DayNum == None:
Day = theTime[6] # extract the day value
elif DayNum == 1 or 2 or 3 or 4 or 5 or 6 :
today = theTime[6]
Day = today + DayNum
else:
print "That is not a valid choice"
return days[Day]``````

This is the code I have made. It aims to produce the name of the day of the week basing everything on the time module. It is called through dayOfWeek().
I would like to know if it was possible to create a looping list...that is to say if today was Wednesday.. i would type dayOfWeek() and it would return Wednesday.. but say I wanted to know the name of the day in 20 days..if i write dayOfWeek(20) it will give me an error because the list is only day[0:6] How would I do this?

regards,

Jeremy.

jrcagle 77

"mod" is your friend. The idea behind mod n is that it simulates going around a clock with n hours. The arithmetic is simple: mod divides by n and takes the remainder:

>>> 23 % 5
3

(because 23/5 is 4 remainder 3)

Which is equivalent to saying, "On a five-hour clock, what time is 23 o'clock?" Answer: 3.

So:

``````def DayOfWeek(offset = 0):
days = ['Monday','Tuesday',
'Wednesday','Thursday',
'Friday','Saturday','Sunday']
theTime = time.localtime(time.time())

today = theTime[6]
return days[(today + offset) % 7]``````

Hope it helps!
Jeff

jem00

So does the code you have written works as efective as mine? Do I have to put the if loop?

jem00

Don't worry i figured everything out.

Thanks alot.

I'll be back soon with more questions :)

Bye for now