Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Display Results in a Table with Columns

My goal is to print the results from the code below in a table with two columns. So far everything works except the second column. If I run:

print(weekday.count('Mon'))

It comes back with "4" which is exactly what I need for the table.

Here is the code I have right now:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import calendar
from itertools import count

days = 'Mon Tue Wed Thu Fri Sat Sun'.split()
start = 1980
end = 2030
weekday = []
dash = '-' * 40
header = ['Weekday', 'Number of leap days']

for year in range(start, end):
    if calendar.isleap(year):
        day_weekday = calendar.weekday(year, 2, 29)
        weekday.append(days[day_weekday])

print(dash)
print('{:<10s}{:>20s}'.format(header[0], header[1]))
print(dash)
print('{:<10s}'.format(('Sunday'), weekday.count('Sun')))
print('{:<10s}'.format(('Monday'), weekday.count('Mon')))
print('{:<10s}'.format(('Tuesday'), weekday.count('Tue')))
print('{:<10s}'.format(('Wednesday'), weekday.count('Wed')))
print('{:<10s}'.format(('Thursday'), weekday.count('Thu')))
print('{:<10s}'.format(('Friday'), weekday.count('Fri')))
print('{:<10s}'.format(('Saturday'), weekday.count('Sat')))

Running the code results with the following:

Weekday    Number of leap days
----------------------------------------
Sunday    
Monday    
Tuesday   
Wednesday 
Thursday  
Friday    
Saturday  

Nothing is displaying on the second column and I’ve scoured the internet to correct this. Any assistance will be appreciated and I apologize in advance for the newbie question.

>Solution :

import calendar
from itertools import count

days = 'Mon Tue Wed Thu Fri Sat Sun'.split()
start = 1980
end = 2030
weekday = []
dash = '-' * 40
header = ['Weekday', 'Number of leap days']

for year in range(start, end):
    if calendar.isleap(year):
        day_weekday = calendar.weekday(year, 2, 29)
        weekday.append(days[day_weekday])

print(dash)
print('{:<10s}{:>20s}'.format(header[0], header[1]))
print(dash)
print('{:<10s}{}'.format(('Sunday'), weekday.count('Sun')))
print('{:<10s}{}'.format(('Monday'), weekday.count('Mon')))
print('{:<10s}{}'.format(('Tuesday'), weekday.count('Tue')))
print('{:<10s}{}'.format(('Wednesday'), weekday.count('Wed')))
print('{:<10s}{}'.format(('Thursday'), weekday.count('Thu')))
print('{:<10s}{}'.format(('Friday'), weekday.count('Fri')))
print('{:<10s}{}'.format(('Saturday'), weekday.count('Sat')))

output

----------------------------------------
Weekday    Number of leap days
----------------------------------------
Sunday    1
Monday    2
Tuesday   2
Wednesday 2
Thursday  2
Friday    2
Saturday  2
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading