简体   繁体   English

如何在 Python 中制作具有自定义日期范围和自定义开始日期的日历

[英]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我需要创建一个自定义日历,这些是星期几 = 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.当前使用带有范围的 for 语句。

I also need to find out how to make the a new line after 15 numbers我还需要找出如何在 15 个数字后创建一个新行

# 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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