I have a long piece of code that has many repeating points. I am sure that it doesn't take this amount of code to create a simple calendar, but I do not know how to shorten this piece of code.
# Creating a calendar in proportion to the user defined starting date
n = int(input('Enter number of days: '))#days in a month
s = input('The first day of the week: ')#The date that user wants the month to start
s = s.capitalize()
lis1 = ['Sa','M','T','W','Th','F','Su']
print('{:>3}{:>4}{:>4}{:>4}{:>4}{:>4}{:>4}'.format('Sa','M','T','W','Th','F','Su'))
for i in range(1,n+1):#loop to generate the dates for a month
if s==lis1[0] and i ==1:#this is to determine how much should the code indent to align to traditional calendar formatting
print('{:>3}'.format(i),end =' ')
elif s==lis1[1] and i ==1:
print(' '*4+'{:>3}'.format(i),end =' ')
elif s==lis1[2] and i ==1:
print(' '*8+'{:>3}'.format(i),end =' ')
elif s==lis1[3] and i ==1:
print(' '*12+'{:>3}'.format(i),end =' ')
elif s==lis1[4] and i ==1:
print(' '*16+'{:>3}'.format(i),end =' ')
elif s==lis1[5] and i ==1:
print(' '*20+'{:>3}'.format(i),end =' ')
elif s==lis1[6] and i ==1:
print(' '*24+'{:>3}'.format(i),end =' ')
else:
print('{:>3}'.format(i),end =' ')#after the indent this is to print the remaining dates
if s==lis1[0] and i%7==0:#this is to print a new line in proportion the choosen starting date so as to stay in check with the traditional calendar format
print()
elif s==lis1[1] and i%7==6:
print()
elif s==lis1[2] and i%7==5:
print()
elif s==lis1[3] and i%7==4:
print()
elif s==lis1[4] and i%7==3:
print()
elif s==lis1[5] and i%7==2:
print()
elif s==lis1[6] and i%7==1:
print()
This is an example of the calendar I am outputting.
Please guide me on shortening code that is related to loops if possible.
calendar
module. For example a simple text calendar for May 2010 with Sunday as the start of the week could be:import calendar; calendar.setfirstweekday(6); print(calendar.month(2010, 5))
\$\endgroup\$ – Peilonrayz Jul 5 '17 at 14:57