第八章作业
P301
8.1
import random
def ping_pong_match():
player1_score = 0
player2_score = 0
while True:
# 随机决定得分方
winner = random.choice([1, 2])
if winner == 1:
player1_score += 1
else:
player2_score += 1
if (player1_score >= 11 or player2_score >= 11) and abs(player1_score - player2_score) >= 2:
break
if player1_score > player2_score:
return "Player 1 wins"
else:
return "Player 2 wins"
if name == "main":
result = ping_pong_match()
print(result)
8.2
import random
def basketball_match():
team1_score = 0
team2_score = 0
for _ in range(48):
# 随机决定进攻方和得分
attack_team = random.choice([1, 2])
score = random.randint(0, 3)
if attack_team == 1:
team1_score += score
else:
team2_score += score
if team1_score > team2_score:
return "Team 1 wins"
elif team1_score < team2_score:
return "Team 2 wins"
else:
return "Draw"
if name == "main":
result = basketball_match()
print(result)
8.3
import os
video_url = "https://www.bilibili.com/video/XXXXXX" # 替换为真实视频链接
os.system(f"you - get {video_url}")
8.4
from direct.showbase.ShowBase import ShowBase
from panda3d.core import *
class MyApp(ShowBase):
def init(self):
ShowBase.init(self)
# 加载一个立方体模型
model = self.loader.loadModel("models/misc/box")
model.reparentTo(self.render)
app = MyApp()
app.run()
8.5
import networkx as nx
import matplotlib.pyplot as plt
创建一个无向图
G = nx.Graph()
添加节点
G.add_nodes_from([1, 2, 3])
添加边
G.add_edges_from([(1, 2), (2, 3)])
绘制图形
nx.draw(G, with_labels=True)
plt.show()
8.6
from PIL import Image, ImageFilter
def emboss_image(image_path):
image = Image.open(image_path)
# 使用浮雕滤镜
embossed_image = image.filter(ImageFilter.EMBOSS)
return embossed_image
if name == "main":
image_path = "your_image.jpg" # 替换为实际图片路径
result = emboss_image(image_path)
result.show()
8.7
from PIL import Image, ImageFilter
def blur_image(image_path):
image = Image.open(image_path)
# 使用模糊滤镜
blurred_image = image.filter(ImageFilter.BLUR)
return blurred_image
if name == "main":
image_path = "your_image.jpg" # 替换为实际图片路径
result = blur_image(image_path)
result.show()