今天是母亲节,笔者在这里祝天下所有的母亲节日快乐,母亲为孩子撑起一片天,是孩子心中的永恒的星。那么今天我就用一个Python程序表达这份祝福。效果如下:

母亲节 python作品 母亲节文案代码_Python

以下是代码及注释

import os
import time
from random import randint


for i in range(1, 35):  # 打印抬头
    print('')

heartStars = [2,4,8,10,14,20,26,28,40,44,52,60,64,76]  # *的位置
heartBreakLines = [13, 27, 41, 55, 69, 77]  # 空格的位置
flowerBreakLines = [7, 15, 23, 31, 39, 46]#玫瑰的空列位置

def addSpaces(a):#添加空列
    count = a
    while count > 0:
        print(' ', end='')
        count -= 1


def newLineWithSleep():#添加空行
    time.sleep(0.3)
    print('\n', end='')


play = 0
while play == 0:
    Left_Spaces = randint(8, 80)
    addSpaces(Left_Spaces)

    for i in range(0, 78):#比心的形状
        if i in heartBreakLines:
            newLineWithSleep()
            addSpaces(Left_Spaces)
        elif i in heartStars:
            print('*', end='')
        elif i in (32, 36):
            print('M', end='')
        elif i == 34:
            print('O', end='')
        else:
            print(' ', end='')

    newLineWithSleep()
    addSpaces(randint(8, 80))
    print("H a p p y  M o t h e r ' s   D a y !", end='')
    newLineWithSleep()
    newLineWithSleep()



    Left_Spaces = randint(8, 80)
    addSpaces(Left_Spaces)
    for i in range(0, 47):#向母亲献花
        if i in flowerBreakLines:
            newLineWithSleep()
            addSpaces(Left_Spaces)
        elif i in (2, 8, 12, 18):
            print('{', end='')
        elif i in (3, 9, 13, 19):
            print('_', end='')
        elif i in (4, 10, 14, 20):
            print('}', end='')
        elif i in (27, 35, 43):
            print('|', end='')
        elif i in (34, 44):
            print('~', end='')
        elif i == 11:
            print('o', end='')
        else:
            print(' ', end='')

    print('\n', end='')  # 儿子给您的祝福

再次向所有伟大的母亲致敬!