第13周

8.1
from random import random def printintro(): print("这个程序模拟两个选手A和B的某种竞技比赛") print("程序运行需要A和B的能力值(用0到1之间的小数表示)") def getinputs(): a = eval(input("请输入选手A的能力值:")) b = eval(input("请输入选手B的能力值:")) n = eval(input("模拟比赛的场次:")) return a, b, n def simngames(n, probA, probB): winsA, winsB = 0, 0 for i in range(n): scoreA, scoreB = simonegame(probA, probB) if scoreA>scoreB: winsA += 1 else: winsB += 1 return winsA, winsB def gameover(a, b): return a==11 or b==11 def simonegame(probA, probB): scoreA, scoreB = 0, 0 serving = "A" while not gameover(scoreA, scoreB): if serving == "A": if random() < probA: scoreA += 1 else: serving = "B" else: if random() < probB: scoreB += 1 else: serving = "A" return scoreA, scoreB def printsummary(winsA, winsB): n = winsA + winsB print("竞技分析开始,共模拟{}场比赛".format(n)) print("选手A获胜{}场比赛,占比{:0.1%}".format(winsA, winsA/n)) print("选手B获胜{}场比赛,占比{:0.1%}".format(winsB, winsB/n)) def main(): printintro() probA, probB, n = getinputs() winsA, winsB = simngames(n, probA, probB) printsummary(winsA, winsB) main()
8.2
import random def printIntro(): print("这个程序模拟量个选手A和B的篮球竞技比赛") print("程序运行需要A和B的能力值(以0到1之间的小数表示)") def getInputs(): a = eval(input("请输入选手A的能力值(0-1): ")) b = eval(input("请输入选手B的能力值(0-1): ")) n = eval(input("模拟比赛的场次: ")) return a, b, n def printSummary(winsA, winsB): n = winsA + winsB print("竞技分析开始, 共模拟{}场比赛".format(n)) print("选手A获胜{}场比赛, 占比{:0.1%}".format(winsA, winsA/n)) print("选手B获胜{}场比赛, 占比{:0.1%}".format(winsB, winsB/n)) def gameOver(quarters): return quarters>=4 def simOneGame(probA, probB): scoreA, scoreB = 0, 0 possesion= "A" quarters=0 while not gameOver(quarters): quarterstime=12*60 while quarterstime>0: quarterstime-=24 if possesion == "A": if random.random() < probA: scoreA += random.choice([2,3]) possesion="B" else: if random.random() < probB: scoreB += random.choice([2,3]) possesion="A" quarters+=1 return scoreA, scoreB def simNGames(n ,probA, probB): winsA, winsB = 0, 0 for i in range(n): scoreA, scoreB = simOneGame(probA, probB) if scoreA > scoreB: winsA += 1 else: winsB += 1 return winsA, winsB def main(): printIntro() probA, probB, n = getInputs() winsA, winsB = simNGames(n, probA, probB) printSummary(winsA, winsB) main()
8.3
import os print ("pip install you-get") os.system("you-get https://www.bilibili.com/video/BV1GJ411x7h7") os.system("you-get --format=hd -o ./videos https://www.youtube.com/watch?v=dQw4w9WgXcQ")
8.4
from direct.showbase.ShowBase import ShowBase from panda3d.core import loadPrcFileData loadPrcFileData("", "window-title My First Panda3D Game") loadPrcFileData("", "win-size 800 600") class MyApp(ShowBase): def init(self): ShowBase.init(self) self.environ = self.loader.loadModel("models/environment") self.environ.reparentTo(self.render) self.environ.setScale(0.25, 0.25, 0.25) self.environ.setPos(-8, 42, 0) app = MyApp() app.run()
8.5
import networkx as nx import matplotlib.pyplot as plt G = nx.Graph() nx.draw(G)
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()

posted @ 2025-05-25 21:19  飕飕  阅读(14)  评论(0)    收藏  举报