Hi! I'm trying to program a monthly calendar.... but I can't get the weekdays for the nth day
of the year right... my problem is written further down in the code...

``````from time import *

class Calendar:
'''Skapar en kalender för varje månad'''
def __init__(self, year, month):
self.year = year
self.month = month
from time import *

class Calendar:
'''Skapar en kalender för varje månad'''
def __init__(self, year, month):
self.year = year
self.month = month

def leap_year(self, year):
'''Metod som tar reda på om året är ett skottår'''
if (year % 4 == 0) and not(year % 100 == 0)or (year % 400 == 0):
return year
else:
return None

def weekday(self, year, weekday):
'''Metod som tar fram veckodagen för varje dag.'''
d = weekday
if year:
d += (year-1900) * 365
for i in range(1900, year):
if self.leap_year(i):
d += 1

My problem starts here..
#d = the amount of days from year 1900 until chosen year. Now I want each of these days to be given a weekday number from 0-6...starting from day 1 year 1900 which is a monday
(weekdays[0]), until day d which is the last day of the chosen year.... How do i do this? I have tried some things here: But I know they don't really work..can someone please help me?!

for i in range(0, d):
if (i % 7 == 0)or (i == 0):
i -= i
while i < 7:
i += 1
w = weekdays[i-1]
return w
and if (i % 7 == 0) and (i >= (d-6)):
print weekdays[(d-i)]

#Mainprogram

months = ['januari', 'februari', 'mars', 'april', 'maj', 'juni', 'juli', 'augusti',\
'september', 'oktober', 'november', 'december']

weekdays = ['Må', 'Ti', 'On', 'To', 'Fr', 'Lö', 'Sö']

days = [31,28,31,30,31,30,31,31,30,31,30,31]

def write_calendar(days_until):
'''Skriver ut kalendern'''

'''Skriver ut menyn samt anropar metoder och funktioner för respektive alternativ.'''

choice = None
while choice != '3':
print \
'''
(1) See this months calendar
(2) Choose year and month
(3) End
'''
choice = raw_input('Which alternative do you choose?')

if choice == '1':
today = localtime()
this_month = Calendar(today[0], today[1])
this_month.leap_year(today[0])
this_month.weekday(today[0], 0)
a = 0
for i in range(0, (today[1]-1)):
a += days[i]

elif choice =='2':
chosen_year = input(What year would you like to see?')
chosen_month = input(What month would you like to see?')
your_choice = Calendar(chosen_year, chosen_month)
your_choice.leap_year(chosen_year)
your_choice.weekday(chosen_year, 0)
a = 0
for i in range(0, (chosen_month-1)):
a += days[i]

Hi! I'm trying to program a monthly calendar.... but I can't get the weekdays for the nth day
of the year right...

Have you looked far enough into the time module? The tm_wday element of the struct_time is exactly what you want.

Or is there some reason you can't use that?

Have you looked far enough into the time module? The tm_wday element of the struct_time is exactly what you want.

Or is there some reason you can't use that?

Haven't looked there for it, but I have a task that says I should use the 1 of january year 1900 as a constant in the program to calculate the weekdays for all other days...

Have you tested the code below to see if it does what you think. I don't know myself but it is unclear at best and if you have to explain it, will you be able. The following untested code breaks it down into steps

``````def leap_year(self, year):
if (year % 4 == 0) and not(year % 100 == 0)or (year % 400 == 0):
#
##------- Breaking it down into steps  ---------------------------
# eliminate this first
if year % 400 == 0:     ## is leap year
return 1
##   has to be divisible by 4
if year % 4 == 0:
# if divisible by 4 and 100 then it is not a leap year
if (year % 100 == 0):
return 0
else:
return 1
return 0     ## not a leap year-->not divisible by 4 or 400``````

As for the days of the week, is the variable d the number of days from 1900? And is the first day in d --> weekday=0, and d==2 --> weekday=1, so then d==8 --> weekday=0 = start over again? If so you should be able to use divmod(d-1, 7) with the remainder being the day of the week.

For those of us who were around before there were canned date/time libraries, Zeller's Congruence was common knowledge http://en.wikipedia.org/wiki/Zeller's_congruence but it would have to be modified by comparing the actual day of the week of 01-01-1900 and what you would have to add or subtract to get 01-01-1900 to be a Monday.

I just noticed this. After the final line of this snippet, i will always be zero if it was greater than zero, and -(2i) if it was less than zero. Is that what you are trying to do?

``````for i in range(0, d):
if (i % 7 == 0)or (i == 0):
i -= i``````

You might be able to use this ...

``````# this gives daily info:
# takes care of leap-years too

import datetime as dt

wday = ['Monday', 'Tuesday', 'Wednesday', 'Thursday',
'Friday', 'Saturday', 'Sunday']

start = dt.date(1900, 1, 1)

oneday = dt.timedelta(days=1)

# test it for the first 370 days
for x in range(370):
print start.year, start.month, start.day, wday[start.weekday()]
start += oneday``````

Have you tested the code below to see if it does what you think. I don't know myself but it is unclear at best and if you have to explain it, will you be able. The following untested code breaks it down into steps

``````def leap_year(self, year):
if (year % 4 == 0) and not(year % 100 == 0)or (year % 400 == 0):
#
##------- Breaking it down into steps  ---------------------------
# eliminate this first
if year % 400 == 0:     ## is leap year
return 1
##   has to be divisible by 4
if year % 4 == 0:
# if divisible by 4 and 100 then it is not a leap year
if (year % 100 == 0):
return 0
else:
return 1
return 0     ## not a leap year-->not divisible by 4 or 400``````

As for the days of the week, is the variable d the number of days from 1900? And is the first day in d --> weekday=0, and d==2 --> weekday=1, so then d==8 --> weekday=0 = start over again? If so you should be able to use divmod(d-1, 7) with the remainder being the day of the week.

The leap year method works as it should.. and yes I want the days d to be given numbers from 0-6 just like you explained above......how does divmod(d-1, 7) work?

I just noticed this. After the final line of this snippet, i will always be zero if it was greater than zero, and -(2i) if it was less than zero. Is that what you are trying to do?

``````for i in range(0, d):
if (i % 7 == 0)or (i == 0):
i -= i``````

I was trying to split the days up into weeks, and then by saying every seventh day will be zero, I could give every day a number from 0-6.... but this just keeps looping, and I don't know how to do to make it stop when i == d

You might be able to use this ...

``````# this gives daily info:
# takes care of leap-years too

import datetime as dt

wday = ['Monday', 'Tuesday', 'Wednesday', 'Thursday',
'Friday', 'Saturday', 'Sunday']

start = dt.date(1900, 1, 1)

oneday = dt.timedelta(days=1)

# test it for the first 370 days
for x in range(370):
print start.year, start.month, start.day, wday[start.weekday()]
start += oneday``````

I tried something like this... but I don't get it to work...

``````from time import *
import datetime as dt
start = dt.date(1900, 1, 1)
oneday = dt.timedelta(days=1)

class Calendar:
'''Skapar en kalender för varje månad'''
def __init__(self, year, month):
self.year = year
self.month = month

def leap_year(self, year):
'''Metod som tar reda på om året är ett skottår'''
if (year % 4 == 0) and not(year % 100 == 0)or (year % 400 == 0):
return year
else:
return None

def weekday(self, year, days):
'''Metod som tar fram veckodagen för varje dag.'''
d = days
if year:
d += (year-1900) * 365
for i in range(1900, year+1):
if self.leap_year(i):
d += 1
print d
for i in range(0, d):
print weekdays[start.weekday()]
start += oneday``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.