简体   繁体   中英

How to make a calendar in Python with a custom date range and custom start day of the month

I need to create a custom calendar, these are the days of the week= Po Si Be Ri Ve An Ca Mi Fo Ar De Pr Sp Th Re

Next I need to find a way to fix the spacing so that it looks like a calendar.

Currently using a for statement with a range.

I also need to find out how to make the a new line after 15 numbers

# Create a function where you create the heading for the calendar
# Create input questions
import numpy as np

#dayofweek = input("What day of the week does the 1st day of the month start on? ")
numdays = int(input("How many days are there in the month? "))
#jubmonth = input("Is this a Jubilee Month ? ")


day = "Po  Si  Be  Ri  Ve  An  Ca  Mi  Fo  Ar  De  Pr  Sp  Th  Re"
print(day)

# Create a function where you create the list of numbers from 1 to x = numdays
x = numdays
y =range(1,x)

for elm in y:
  print('',elm, end10='  ')
else:
    print(elm, end='  ')



#print(dayofweek)
#print(numdays)
#print(jubmonth)
#print(y)

Not sure if i understood your requirement correctly but assuming you want a table style output for your calendar that will be evenly spaced you could use the below code. I have updated the code to cater for jubilee months

def print_cal(cal):
    for line in cal:
        print("".join([f'{str(item):>3s}' for item in line]))

while True:
    # set up days , jubilee days, jubilee weeks
    days = ['Po', 'Si', 'Be', 'Ri', 'Ve', 'An', 'Ca', 'Mi', 'Fo', 'Ar', 'De', 'Pr', 'Sp', 'Th', 'Re']
    days_size = len(days)
    jubilee_days = ['Be', 'An', 'Ca', 'Ar', 'De', 'Pr']
    jubilee_week = [2, 3]
    filler = '--'

    # get user input
    num_days = int(input("How many days are there in the month? "))
    day_of_week = input("What day of the week does the 1st day of the month start on? ")
    jub_month = input("Is it a Jub month (yes/no)? ").lower()

    # work out which day to start on write our days as the first line then skip any days before our start day
    day_index = days.index(day_of_week)
    cal = [days]
    cal.append(['' for _ in range(day_index)])

    # now in a loop generate the days
    for i in range(0, num_days):
        # if the last list is full start a new one.
        if len(cal[-1]) == days_size:
            cal.append([])

        #check for jub month
        if jub_month == "yes" and len(cal) - 1 in jubilee_week:
            #check for jub day and keep looping till its not a jub day
            day = days[len(cal[-1])]
            while day in jubilee_days:
                cal[-1].append(filler)
                day = days[len(cal[-1])]

        #write the day to the last list
        cal[-1].append(i + 1)

    #print the cal
    print_cal(cal)
    again = input("Do you want to make another cal (yes/no)? ").lower()
    if again != "yes":
        break


OUTPUT

How many days are there in the month? 45
What day of the week does the 1st day of the month start on? Ve
Is it a Jub month (yes/no)? no
 Po Si Be Ri Ve An Ca Mi Fo Ar De Pr Sp Th Re
              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 29 30 31 32 33 34 35 36 37 38 39 40 41
 42 43 44 45
Do you want to make another cal (yes/no)? yes
How many days are there in the month? 45
What day of the week does the 1st day of the month start on? Ve
Is it a Jub month (yes/no)? yes
 Po Si Be Ri Ve An Ca Mi Fo Ar De Pr Sp Th Re
              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 29
 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
 45
Do you want to make another cal (yes/no)? no

Process finished with exit code 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM