1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2014
Posts
6
Rep Power
0

#### For-loop calendar help!

sorry about how messy it is but I cant figure out a way to get the for-loop to work, everything is working but the the for-loop

I am trying to print out the calendar

Code:
```

#Define number of days in a month and put in a list.
days_array= [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

#Define month names
month_array= ['January' , 'February' , 'March' , 'April' , 'May' , 'June' , 'July' , 'August' , 'September' , 'October' , 'November' , 'December']

#Get user input until they choose to quit
while True:
#prompt for month
month_input= int(input("\nEnter the month (1-12): "))
if(month_input==0):
break
#prompt for year
year_input= int(input("\nEnter the year (after 1900): "))
if(year_input>1900) :
break
#Determine what day January of this year starts
starting_day_of_year= int(((year-1901) + (year-1901)/4+2)%7)
#determine what day current month starts
#align month with index
month_input= month_input-1
starting_day_of_month= int((starting_day_of_year + sum(days_array[0: month_input]))%7)
#adjust for March-December of leap year
if (month_input >=2 & year%4== 0):
starting_day_of_month= int((starting_day_of_month +1)%7)
#print month, name & date
#for-loop print each date of the month
:flame: NEED HELP HERE
for i in range(days_array)(month_array)
r just (4), end = "")
#print Feb 29th if necessary

if (month_input== 1 & year%4==0 ):
print ("  29")```
2. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
May 2013
Location
/dev/null
Posts
163
Rep Power
19
1. You're breaking out of the while loop if (year_input > 1900). I think you meant (year_input < 1900). BTW, the parenthesis are redundant in this part of python code.

2. Shouldn't the "starting_day_of_year" calculation have (year_input -1901) instead of (year - 1901) ?

3. The for-loop part of the code is really messy! I'm not able to understand what you're trying to achieve here. Could you please correct the code in your post (make it more legible :-) ).
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2014
Posts
6
Rep Power
0
Originally Posted by noobie1000
1. You're breaking out of the while loop if (year_input > 1900). I think you meant (year_input < 1900). BTW, the parenthesis are redundant in this part of python code.

2. Shouldn't the "starting_day_of_year" calculation have (year_input -1901) instead of (year - 1901) ?

3. The for-loop part of the code is really messy! I'm not able to understand what you're trying to achieve here. Could you please correct the code in your post (make it more legible :-) ).
Actually I do no have any syntax error when it comes to 1 and 2, the program actually runs just fine

for 3 I didn't even start on the for-loop that is why I am posting in here to seek help!
4. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
May 2013
Location
/dev/null
Posts
163
Rep Power
19
Point #1 and #2 do not refer to syntax errors. They're errors in logic.
Code:
```if (year_input > 1900): # here you're checking if the user has entered year more than 1900
break # if he did, then break out of the while loop!```
Code:
`starting_day_of_year= int(((year-1901) + (year-1901)/4+2)%7)`
You've not defined the variable year anywhere before in the code you posted. Is this year_input?

And, if this is not specifically a homework (or self learning) assignment, then I would suggest you to use the time module rather than working through math to display dates.
5. #### Works with a few changes and output section inserted

Program produces correct result for February, 2014.
Code:
```# new comments mark changes and contain dwl, for which you can search

'''
\$ cal  ################ reference data
February 2014
Su Mo Tu We Th Fr Sa
1
2  3  4  5  6  7  8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28

\$ python3 p.py  ################ new python program result

Enter the month (1-12): 2

Enter the year (after 1900): 2014
Calendar for February 2014
1
2   3   4   5   6   7   8
9  10  11  12  13  14  15
16  17  18  19  20  21  22
23  24  25  26  27  28

Enter the month (1-12): 0
\$
'''

#Define number of days in a month and put in a list.
days_array= [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

#Define month names
month_array= 'January February March April May June July August September October November December'.split() #### dwl  easier!

#Get user input until they choose to quit
while True:
#prompt for month
month_input= int(input("\nEnter the month (1-12): "))
if not month_input:
break
#prompt for year
year_input= int(input("\nEnter the year (after 1900): "))
if year_input < 1901:
break
year = year_input################ dwl
#Determine what day January of this year starts
starting_day_of_year= int(((year-1901) + (year-1901)/4+2)%7)
#determine what day current month starts
#align month with index
month = month_input - 1 #### dwl
starting_day_of_month= int((starting_day_of_year + sum(days_array[:month]))%7)
#adjust for March-December of leap year
if (month >=2 and year%4== 0):  ####dwl & is not and  http://docs.python.org/3/reference/expressions.html#index-76
starting_day_of_month= int((starting_day_of_month +1)%7)
##### dwl to end
print('Calendar for', month_array[month], year)#    #print month, name & date
leap_February = (month_input == 2) and (year%4 == 0) and (year%400 != 0)  #### dwl use your own formula

i = 0
start = starting_day_of_month
end = starting_day_of_month + days_array[month] + leap_February
for week in range(6):
out = []
for day in range(7):
out.append('{:4d}'.format(i+1-start) if start <= i < end else ' '*4)
i += 1
print(''.join(out))```
6. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2014
Posts
6
Rep Power
0
Originally Posted by b49P23TIvg
Program produces correct result for February, 2014.
Code:
```# new comments mark changes and contain dwl, for which you can search

'''
\$ cal  ################ reference data
February 2014
Su Mo Tu We Th Fr Sa
1
2  3  4  5  6  7  8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28

\$ python3 p.py  ################ new python program result

Enter the month (1-12): 2

Enter the year (after 1900): 2014
Calendar for February 2014
1
2   3   4   5   6   7   8
9  10  11  12  13  14  15
16  17  18  19  20  21  22
23  24  25  26  27  28

Enter the month (1-12): 0
\$
'''

#Define number of days in a month and put in a list.
days_array= [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

#Define month names
month_array= 'January February March April May June July August September October November December'.split() #### dwl  easier!

#Get user input until they choose to quit
while True:
#prompt for month
month_input= int(input("\nEnter the month (1-12): "))
if not month_input:
break
#prompt for year
year_input= int(input("\nEnter the year (after 1900): "))
if year_input < 1901:
break
year = year_input################ dwl
#Determine what day January of this year starts
starting_day_of_year= int(((year-1901) + (year-1901)/4+2)%7)
#determine what day current month starts
#align month with index
month = month_input - 1 #### dwl
starting_day_of_month= int((starting_day_of_year + sum(days_array[:month]))%7)
#adjust for March-December of leap year
if (month >=2 and year%4== 0):  ####dwl & is not and  http://docs.python.org/3/reference/expressions.html#index-76
starting_day_of_month= int((starting_day_of_month +1)%7)
##### dwl to end
print('Calendar for', month_array[month], year)#    #print month, name & date
leap_February = (month_input == 2) and (year%4 == 0) and (year%400 != 0)  #### dwl use your own formula

i = 0
start = starting_day_of_month
end = starting_day_of_month + days_array[month] + leap_February
for week in range(6):
out = []
for day in range(7):
out.append('{:4d}'.format(i+1-start) if start <= i < end else ' '*4)
i += 1
print(''.join(out))```
This is exactly what I been looking for. Thanks, unfortunately I can't run python on my own personal laptop just because I can't seem to install it correctly but I will get back to you once I am back at school. Thanks again.
7. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2014
Posts
1
Rep Power
0

#### hani dalqamouni here!

hello everybody! i am hani dalqamouni...
i am new to this forum annd i am happy to join here
to meet new friends and to share interests with you guys!

cheers,
hani dalqamouni
8. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2014
Posts
6
Rep Power
0
Originally Posted by b49P23TIvg
Program produces correct result for February, 2014.
Code:
```# new comments mark changes and contain dwl, for which you can search

'''
\$ cal  ################ reference data
February 2014
Su Mo Tu We Th Fr Sa
1
2  3  4  5  6  7  8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28

\$ python3 p.py  ################ new python program result

Enter the month (1-12): 2

Enter the year (after 1900): 2014
Calendar for February 2014
1
2   3   4   5   6   7   8
9  10  11  12  13  14  15
16  17  18  19  20  21  22
23  24  25  26  27  28

Enter the month (1-12): 0
\$
'''

#Define number of days in a month and put in a list.
days_array= [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

#Define month names
month_array= 'January February March April May June July August September October November December'.split() #### dwl  easier!

#Get user input until they choose to quit
while True:
#prompt for month
month_input= int(input("\nEnter the month (1-12): "))
if not month_input:
break
#prompt for year
year_input= int(input("\nEnter the year (after 1900): "))
if year_input < 1901:
break
year = year_input################ dwl
#Determine what day January of this year starts
starting_day_of_year= int(((year-1901) + (year-1901)/4+2)%7)
#determine what day current month starts
#align month with index
month = month_input - 1 #### dwl
starting_day_of_month= int((starting_day_of_year + sum(days_array[:month]))%7)
#adjust for March-December of leap year
if (month >=2 and year%4== 0):  ####dwl & is not and  http://docs.python.org/3/reference/expressions.html#index-76
starting_day_of_month= int((starting_day_of_month +1)%7)
##### dwl to end
print('Calendar for', month_array[month], year)#    #print month, name & date
leap_February = (month_input == 2) and (year%4 == 0) and (year%400 != 0)  #### dwl use your own formula

i = 0
start = starting_day_of_month
end = starting_day_of_month + days_array[month] + leap_February
for week in range(6):
out = []
for day in range(7):
out.append('{:4d}'.format(i+1-start) if start <= i < end else ' '*4)
i += 1
print(''.join(out))```

I just have one more question how can I add the print line were it prints the days on top of the calendar with the correct spacing?
9. This is very terrible news for me. I had trusted that you wrote the rest of the program. Now I am in deep doubt.

You can't write a print statement????

• noobie1000 agrees : Ha ha ha! I think you just did his homework :-D
10. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2014
Posts
6
Rep Power
0
Originally Posted by b49P23TIvg
This is very terrible news for me. I had trusted that you wrote the rest of the program. Now I am in deep doubt.

You can't write a print statement????
No I actually did write the simple print line and I used str for the spacing, I just keep getting an indent error

print ("mo su tu th we fr sa")

No need to be in doubt my friend lol

Even professor couldn't figure out the error!