Home > database >  Display Results in a Table with Columns
Display Results in a Table with Columns

Time:01-23

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:

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.

CodePudding user response:

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
  •  Tags:  
  • Related