Take you to make 7 programs with Python, let you feel the joy of the Dragon Boat Festival

Name: Ah Yue's Little Dongdong

Learning: Python, C/C++

Home page link: A Yue's Xiaodongdong's blog_CSDN blog-python&&c++ advanced knowledge, essential for Chinese New Year, blogger in the field of C/C++ knowledge explanation

 

Table of contents

foreword

Procedure 1: Making Zongzi

Program 2: Dragon Boat Race

Procedure 3: Wormwood hanging

Procedure 4: Drink realgar wine

Program 5: Challenge the dragon boat game (this is fun)

Program 6: Dragon Boat Festival Poetry Creation

Program 7: Dragon Boat Race

Summarize


 

foreword

The Dragon Boat Festival, also known as the Dragon Boat Festival, is one of the traditional Chinese festivals. The festival was originally to commemorate the great patriotic poet Qu Yuan, and later became a festival to celebrate a good harvest and pray for health and happiness.

The Dragon Boat Festival has a long history and rich cultural connotations. People will eat rice dumplings, race dragon boats, hang wormwood and drink realgar wine on this day to commemorate Qu Yuan's death and pray for health, safety and happiness.

Python programming and Dragon Boat Festival

Python is an advanced, dynamic, object-oriented programming language, which is easy to learn, easy to read and write, and is more and more loved and favored by programmers. In this article, we will learn how to use Python to write some programs about the Dragon Boat Festival.

Procedure 1: Making Zongzi

Zongzi is one of the traditional foods of the Dragon Boat Festival. It is made of glutinous rice and various fillings wrapped in bamboo leaves and steamed. Now, let's learn how to use Python to write a program for making zongzi.

First, we need to import Python's random module in order to randomly select the fillings for the zongzi:

import random

Next, we define a function called "zongzi" to make zongzi. This function accepts two parameters: one is the list of fillings for the zongzi, and the other is the number of zongzi. Inside the function, we use a for loop to make the specified number of zongzi. The specific code is as follows:

def zongzi(filling, count):
    for i in range(count):
        print("正在制作第" + str(i + 1) + "个粽子...")
        random_filling = random.choice(filling)
        print("选择了" + random_filling + "馅")
        print("粽子制作完成!\n")

Finally, we can test this function with the following code:

filling = ['咸肉', '豆沙', '红枣', '蛋黄']
zongzi(filling, 5)

This code will make 5 rice dumplings, and the fillings used are "bacon", "bean paste", "red dates" and "egg yolk". You can add more fillings to the "filling" list as needed.

Program 2: Dragon Boat Race

Dragon boat races are another traditional event of the Dragon Boat Festival. In this competition, competitors sit on dragon boats and paddle vigorously to cross the finish line in the shortest possible time. Now, let's learn how to write a dragon boat race program in Python.

First, we need to import Python's time module in order to control the running time of the program:

import time

Next, we define a function called "row_boat" to allow competitors to row a dragon boat. This function accepts one parameter, which represents the contestant's name. Inside the function, we use the sleep function of the time module to simulate how long the competitors paddle. The specific code is as follows:

def row_boat(name):
    print(name + "开始划龙舟!")
    for i in range(5):
        time.sleep(1)
        print("划了" + str(i + 1) + "下...")
    print(name + "划船结束!")

Finally, we can test this function with the following code:

participants = ['小米', '小明', '小红', '小刚']
print("现在,参赛者们准备就绪,比赛要开始了...")
for participant in participants:
    row_boat(participant)

This code will create a list called "participants" that contains the names of the four contestants. The program will ask each participant to row a dragon boat in turn, and output the rowing time of each participant.

Procedure 3: Wormwood hanging

During the Dragon Boat Festival, people will hang a bunch of wormwood at the door of the house to drive away poisonous insects and diseases. Now, let's learn how to write a wormwood program using Python.

First, we define a function called "hang_ai" to hang wormwood. This function accepts one parameter, indicating the number of wormwood to hang. Inside the function, we use a for loop to hang the specified amount of wormwood, and output the process of hanging wormwood. The specific code is as follows:

def hang_ai(count):
    for i in range(count):
        print("正在挂第" + str(i + 1) + "把艾草...")
        print("艾草挂好了!\n")

Finally, we can test this function with the following code:

hang_ai(3)

This code will hang three bunches of wormwood. You can change the amount in the "count" parameter as needed.

Procedure 4: Drink realgar wine

During the Dragon Boat Festival, people will drink some realgar wine to drive away diseases and poisonous insects. Now, let's learn how to use Python to write a program to drink realgar wine.

First, we define a function called "drink_xionghuang" to drink realgar wine. This function accepts one parameter, which represents the number of drinks. Inside the function, we use a for loop to drink the specified number of times and output the drinking process. The specific code is as follows:

def drink_xionghuang(count):
    for i in range(count):
        print("正在喝第" + str(i + 1) + "杯雄黄酒...")
        print("雄黄酒喝完了!\n")

Finally, we can test this function with the following code:

drink_xionghuang(2)

This code will drink two glasses of realgar wine. You can change the amount in the "count" parameter as needed.

Ok, next, we will dig into how to use Python to implement some interesting activities for the Dragon Boat Festival.

Program 5: Challenge the dragon boat game (this is fun)

In dragon boat races, competitors need

Paddle to surpass the opponent, but in this game, we will challenge your reaction speed and operation ability. Now, let's learn how to write a dragon boat challenge game using Python.

First, we need to import Python's turtle module, and use this module to draw dragon boats and water surfaces. The specific code is as follows:

import turtle

# 绘制龙舟
def draw_boat():
    turtle.penup()
    turtle.goto(-80, 0)
    turtle.pendown()
    turtle.color("red")
    turtle.pensize(5)
    turtle.begin_fill()
    turtle.forward(80)
    turtle.left(120)
    turtle.forward(80)
    turtle.left(120)
    turtle.forward(80)
    turtle.end_fill()

# 绘制水面
def draw_water():
    turtle.penup()
    turtle.goto(-200, -100)
    turtle.pendown()
    turtle.color("blue")
    turtle.pensize(5)
    turtle.begin_fill()
    turtle.forward(400)
    turtle.right(90)
    turtle.forward(200)
    turtle.right(90)
    turtle.forward(400)
    turtle.right(90)
    turtle.forward(200)
    turtle.end_fill()

Next, we need to define a function called "move_boat" to move the dragon boat. This function accepts one parameter, which represents the distance the dragon boat moves. Inside the function, we use the forward function of the turtle module to move the dragon boat. The specific code is as follows:

# 移动龙舟
def move_boat(distance):
    turtle.penup()
    turtle.goto(-80, 0)
    turtle.pendown()
    turtle.color("red")
    turtle.pensize(5)
    turtle.begin_fill()
    turtle.forward(distance)
    turtle.left(120)
    turtle.forward(distance)
    turtle.left(120)
    turtle.forward(distance)
    turtle.end_fill()

Finally, we can test the game with the following code:

import turtle
import random
import time

# 绘制龙舟
def draw_boat():
    ...

# 绘制水面
def draw_water():
    ...

# 移动龙舟
def move_boat(distance):
    ...

# 挑战龙舟游戏
def challenge_boat():
    print("挑战龙舟游戏开始!")
    while True:
        distance = random.randint(10, 30)
        move_boat(distance)
        time.sleep(0.5)

This code will start a program to challenge the dragon boat game, you need to enter "challenge_boat()" in the terminal to start the game. The game will constantly randomly generate the moving distance, you need to react as soon as possible and control the dragon boat to move through the keyboard to surpass the opponent as much as possible.

Program 6: Dragon Boat Festival Poetry Creation

The Dragon Boat Festival is an important traditional festival, and the ancients left many poems describing the Dragon Boat Festival. Now, let's learn how to use Python to write a program that randomly generates Dragon Boat Festival poems.

First, we need to define a function called "poem" to generate Dragon Boat Festival poems. Inside the function, we use Python's random module and an "if" statement to randomly generate different poems. The specific code is as follows:

import random

# 生成端午节诗词
def poem():
    first_line = ["红旗招展迎节日", "艾叶飘香传佳节", "阳光照耀端午节", "龙舟竞渡忆古人"]
    second_line = ["赛龙舟", "挂艾草", "品粽子", "喝雄黄"]
    third_line = ["笑声声声喜气腾", "欢声笑语遍大街", "端午佳节人人忙", "十年一遇吉日来"]
    if random.randint(0, 1) == 0:
        poem = first_line[random.randint(0, 3)] + "," + second_line[random.randint(0, 3)] + "," + third_line[random.randint(0, 3)]
    else:
        poem = third_line[random.randint(0, 3)] + "," + first_line[random.randint(0, 3)] + "," + second_line[random.randint(0, 3)]
    return poem

Finally, we can test this function with the following code:

print(poem())

This code will randomly generate a Dragon Boat Festival poem and output it.

Program 7: Dragon Boat Race

Dragon boat racing is a traditional event of the Dragon Boat Festival. Now, let's learn how to use Python to write a program for dragon boat racing.

First, we define a function called "dragon_boat" to start a dragon boat race. This function accepts one parameter, which represents the number of participating teams. Inside the function, we use the turtle module to draw the dragon boat and the track, and use Python's random module to randomly generate the speed of the dragon boat and the participating teams. The specific code is as follows:

import turtle
import random
import time

# 绘制赛道
def draw_track():
    turtle.penup()
    turtle.goto(-200, -150)
    turtle.pendown()
    turtle.color("black")
    turtle.pensize(2)
    turtle.forward(400)
    turtle.right(90)
    turtle.forward(300)
    turtle.right(90)
    turtle.forward(400)
    turtle.right(90)
    turtle.forward(300)

# 绘制龙舟
def draw_boat():
    turtle.penup()
    turtle.goto(-80, 0)
    turtle.pendown()
    turtle.color("red")
    turtle.pensize(5)
    turtle.begin_fill()
    turtle.forward(80)
    turtle.left(120)
    turtle.forward(80)
    turtle.left(120)
    turtle.forward(80)
    turtle.end_fill()

# 赛龙舟比赛
def dragon_boat(num):
    print("赛龙舟比赛开始!")
    draw_track()
    draw_boat()
    for i in range(num):
        turtle.penup()
        turtle.goto(-180, -100 + i * 50)
        turtle.pendown()
        turtle.color("blue")
        turtle.pensize(5)
        turtle.begin_fill()
        turtle.circle(10)
        turtle.end_fill()
        time.sleep(1)
    max_distance = 0
    winner = None
    for i in range(num):
        speed = random.randint(30, 50)
        distance = random.randint(100, 300)
        current_distance = distance / speed
        if current_distance > max_distance:
            max_distance = current_distance
            winner = i
    print("获胜者是队伍" + str(winner + 1) + ",用时" + str(max_distance) + "秒。")

Finally, we can test this function with the following code:

dragon_boat(4)

This code will start a dragon boat race program, you need to enter "dragon_boat(num)" in the terminal to start the race, where "num" represents the number of participating teams. The program will draw the track and dragon boat, randomly generate the track and the speed of the participating teams, and finally output the winning team and the time.

Summarize

The above are some ideas and implementations about using Python to write Dragon Boat Festival programs. Python has a wealth of modules and libraries that can help us quickly implement various functions. Whether it is drawing graphics, generating random data or making logical judgments, Python can easily complete it. I hope this article has inspired you, and I wish you a happy holiday during the Dragon Boat Festival

Guess you like

Origin blog.csdn.net/m0_64122244/article/details/131319768