zip

实例汇总.zip

温度转换

  1. TempStr = input("请输入带有符号的温度值: ")
  2. if TempStr[-1] in ["F", "f"]:
  3. C = (eval(TempStr[0:-1]) - 32) / 1.8
  4. print("转换后的温度是{:.2f}C".format(C))
  5. elif TempStr[-1] in ["C", "c"]:
  6. F = 1.8 * eval(TempStr[0:-1]) + 32
  7. print("转换后的温度是{:.2f}F".format(F))
  8. else:
  9. print("输入格式错误")
  10. """
  11. 请输入带有符号的温度值: 39C
  12. 转换后的温度是102.20F
  13. """

python 蟒蛇绘制

  1. import turtle
  2. turtle.setup(650, 350, 200, 200)
  3. turtle.penup()
  4. turtle.fd(-250)
  5. turtle.pendown()
  6. turtle.pensize(25)
  7. turtle.pencolor("purple")
  8. turtle.seth(-40)
  9. for i in range(4):
  10. turtle.circle(40, 80)
  11. turtle.circle(-40, 80)
  12. turtle.circle(40, 80 / 2)
  13. turtle.fd(40)
  14. turtle.circle(16, 180)
  15. turtle.fd(40 * 2 / 3)
  16. turtle.done()

image.png

天天向上的力量

  1. dayup = pow(1.001, 365)
  2. daydown = pow(0.999, 365)
  3. print("向上:{:.2f},向下:{:.2f}".format(dayup, daydown))
  4. # 向上:1.44,向下:0.69
  1. dayfactor = 0.019
  2. dayup = pow(1 + dayfactor, 365)
  3. daydown = pow(1 - dayfactor, 365)
  4. print("向上:{:.2f},向下:{:.2f}".format(dayup, daydown))
  5. # 向上:962.89,向下:0.00
  1. dayup = 1.0
  2. dayfactor = 0.01
  3. for i in range(365):
  4. if i % 7 in [6, 0]:
  5. dayup = dayup * (1 - dayfactor)
  6. else:
  7. dayup = dayup * (1 + dayfactor)
  8. print("工作日的力量:{:.2f} ".format(dayup))
  9. # 工作日的力量:4.63
  1. def dayUP(df):
  2. dayup = 1
  3. for i in range(365):
  4. if i % 7 in [6, 0]:
  5. dayup = dayup * (1 - 0.01)
  6. else:
  7. dayup = dayup * (1 + df)
  8. return dayup
  9. dayfactor = 0.01
  10. while dayUP(dayfactor) < 37.78:
  11. dayfactor += 0.001
  12. print("工作日的努力参数是:{:.3f} ".format(dayfactor))
  13. # 工作日的努力参数是:0.019

文本进度条

  1. import time
  2. scale = 10
  3. print("------执行开始------")
  4. for i in range(scale + 1):
  5. a = "*" * i
  6. b = "." * (scale - i)
  7. c = (i / scale) * 100
  8. print("{:^3.0f}%[{}->{}]".format(c, a, b))
  9. time.sleep(0.1)
  10. print("------执行结束------")
  11. """
  12. ------执行开始------
  13. 0 %[->..........]
  14. 10 %[*->.........]
  15. 20 %[**->........]
  16. 30 %[***->.......]
  17. 40 %[****->......]
  18. 50 %[*****->.....]
  19. 60 %[******->....]
  20. 70 %[*******->...]
  21. 80 %[********->..]
  22. 90 %[*********->.]
  23. 100%[**********->]
  24. ------执行结束------
  25. """
  1. import time
  2. for i in range(101):
  3. print("\r{:3}%".format(i), end="")
  4. time.sleep(0.1)
  5. # 100%
  1. import time
  2. scale = 50
  3. print("执行开始".center(scale // 2, "-"))
  4. start = time.perf_counter()
  5. for i in range(scale + 1):
  6. a = "*" * i
  7. b = "." * (scale - i)
  8. c = (i / scale) * 100
  9. dur = time.perf_counter() - start
  10. print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c, a, b, dur), end="")
  11. time.sleep(0.1)
  12. print("\n" + "执行结束".center(scale // 2, "-"))
  13. """
  14. -----------执行开始----------
  15. 100%[**************************************************->]5.22s
  16. -----------执行结束----------
  17. """

身体质量指数 BMI

  1. height, weight = eval(input("请输入身高(米)和体重(公斤)[逗号隔开]: "))
  2. bmi = weight / pow(height, 2)
  3. print("BMI 数值为:{:.2f}".format(bmi))
  4. who = ""
  5. if bmi < 18.5:
  6. who = "偏瘦"
  7. elif 18.5 <= bmi < 25:
  8. who = "正常"
  9. elif 25 <= bmi < 30:
  10. who = "偏胖"
  11. else:
  12. who = "肥胖"
  13. print("BMI 指标为:国际'{0}'".format(who))
  14. """
  15. 请输入身高(米)和体重(公斤)[逗号隔开]: 1.66,66
  16. BMI 数值为:23.95
  17. BMI 指标为:国际'正常'
  18. """
  1. height, weight = eval(input("请输入身高(米)和体重(公斤)[逗号隔开]: "))
  2. bmi = weight / pow(height, 2)
  3. print("BMI 数值为:{:.2f}".format(bmi))
  4. nat = ""
  5. if bmi < 18.5:
  6. nat = "偏瘦"
  7. elif 18.5 <= bmi < 24:
  8. nat = "正常"
  9. elif 24 <= bmi < 28:
  10. nat = "偏胖"
  11. else:
  12. nat = "肥胖"
  13. print("BMI 指标为:国内'{0}'".format(nat))
  14. """
  15. 请输入身高(米)和体重(公斤)[逗号隔开]: 1.66,66
  16. BMI 数值为:23.95
  17. BMI 指标为:国内'正常'
  18. """
  1. height, weight = eval(input("请输入身高(米)和体重(公斤)[逗号隔开]: "))
  2. bmi = weight / pow(height, 2)
  3. print("BMI 数值为:{:.2f}".format(bmi))
  4. who, nat = "", ""
  5. if bmi < 18.5:
  6. who, nat = "偏瘦", "偏瘦"
  7. elif 18.5 <= bmi < 24:
  8. who, nat = "正常", "正常"
  9. elif 24 <= bmi < 25:
  10. who, nat = "正常", "偏胖"
  11. elif 25 <= bmi < 28:
  12. who, nat = "偏胖", "偏胖"
  13. elif 28 <= bmi < 30:
  14. who, nat = "偏胖", "肥胖"
  15. else:
  16. who, nat = "肥胖", "肥胖"
  17. print("BMI 指标为:国际'{0}', 国内'{1}'".format(who, nat))
  18. """
  19. 请输入身高(米)和体重(公斤)[逗号隔开]: 1.66,66
  20. BMI 数值为:23.95
  21. BMI 指标为:国际'正常', 国内'正常'
  22. """

圆周率的计算

  1. pi = 0
  2. N = 100
  3. for k in range(N):
  4. pi += (
  5. 1
  6. / pow(16, k)
  7. * (4 / (8 * k + 1) - 2 / (8 * k + 4) - 1 / (8 * k + 5) - 1 / (8 * k + 6))
  8. )
  9. print("圆周率值是: {}".format(pi))
  10. # 圆周率值是: 3.141592653589793
  1. from random import random
  2. from time import perf_counter
  3. DARTS = 1000 * 1000
  4. hits = 0.0
  5. start = perf_counter()
  6. for i in range(1, DARTS + 1):
  7. x, y = random(), random()
  8. dist = pow(x**2 + y**2, 0.5)
  9. if dist <= 1.0:
  10. hits = hits + 1
  11. pi = 4 * (hits / DARTS)
  12. print("圆周率值是: {}".format(pi))
  13. print("运行时间是: {:.5f}s".format(perf_counter() - start))
  14. """
  15. 圆周率值是: 3.141568
  16. 运行时间是: 0.25680s
  17. """

七段数码管绘制

  1. import turtle
  2. def drawLine(draw): # 绘制单段数码管
  3. turtle.pendown() if draw else turtle.penup()
  4. turtle.fd(40)
  5. turtle.right(90)
  6. def drawDigit(digit): # 根据数字绘制七段数码管
  7. drawLine(True) if digit in [2, 3, 4, 5, 6, 8, 9] else drawLine(False)
  8. drawLine(True) if digit in [0, 1, 3, 4, 5, 6, 7, 8, 9] else drawLine(False)
  9. drawLine(True) if digit in [0, 2, 3, 5, 6, 8, 9] else drawLine(False)
  10. drawLine(True) if digit in [0, 2, 6, 8] else drawLine(False)
  11. turtle.left(90)
  12. drawLine(True) if digit in [0, 4, 5, 6, 8, 9] else drawLine(False)
  13. drawLine(True) if digit in [0, 2, 3, 5, 6, 7, 8, 9] else drawLine(False)
  14. drawLine(True) if digit in [0, 1, 2, 3, 4, 7, 8, 9] else drawLine(False)
  15. turtle.left(180)
  16. turtle.penup()
  17. turtle.fd(20)
  18. def drawDate(date): # 获得要输出的数字
  19. for i in date:
  20. drawDigit(eval(i)) # 通过eval()函数将数字变为整数
  21. def main():
  22. turtle.setup(800, 350, 200, 200)
  23. turtle.penup()
  24. turtle.fd(-300)
  25. turtle.pensize(5)
  26. drawDate("20181010")
  27. turtle.hideturtle()
  28. turtle.done()
  29. main()

image.png

  1. import turtle, time
  2. def drawGap(): # 绘制数码管间隔
  3. turtle.penup()
  4. turtle.fd(5)
  5. def drawLine(draw): # 绘制单段数码管
  6. drawGap()
  7. turtle.pendown() if draw else turtle.penup()
  8. turtle.fd(40)
  9. drawGap()
  10. turtle.right(90)
  11. def drawDigit(d): # 根据数字绘制七段数码管
  12. drawLine(True) if d in [2, 3, 4, 5, 6, 8, 9] else drawLine(False)
  13. drawLine(True) if d in [0, 1, 3, 4, 5, 6, 7, 8, 9] else drawLine(False)
  14. drawLine(True) if d in [0, 2, 3, 5, 6, 8, 9] else drawLine(False)
  15. drawLine(True) if d in [0, 2, 6, 8] else drawLine(False)
  16. turtle.left(90)
  17. drawLine(True) if d in [0, 4, 5, 6, 8, 9] else drawLine(False)
  18. drawLine(True) if d in [0, 2, 3, 5, 6, 7, 8, 9] else drawLine(False)
  19. drawLine(True) if d in [0, 1, 2, 3, 4, 7, 8, 9] else drawLine(False)
  20. turtle.left(180)
  21. turtle.penup()
  22. turtle.fd(20)
  23. def drawDate(date):
  24. turtle.pencolor("red")
  25. for i in date:
  26. if i == "-":
  27. turtle.write("年", font=("Arial", 18, "normal"))
  28. turtle.pencolor("green")
  29. turtle.fd(40)
  30. elif i == "=":
  31. turtle.write("月", font=("Arial", 18, "normal"))
  32. turtle.pencolor("blue")
  33. turtle.fd(40)
  34. elif i == "+":
  35. turtle.write("日", font=("Arial", 18, "normal"))
  36. else:
  37. drawDigit(eval(i))
  38. def main():
  39. turtle.setup(800, 350, 200, 200)
  40. turtle.penup()
  41. turtle.fd(-350)
  42. turtle.pensize(5)
  43. # drawDate('2018-10=10+')
  44. drawDate(time.strftime("%Y-%m=%d+", time.gmtime()))
  45. turtle.hideturtle()
  46. turtle.done()
  47. main()

image.png

科赫雪花小包裹

  1. import turtle
  2. def koch(size, n):
  3. if n == 0:
  4. turtle.fd(size)
  5. else:
  6. for angle in [0, 60, -120, 60]:
  7. turtle.left(angle)
  8. koch(size / 3, n - 1)
  9. def main():
  10. turtle.setup(800, 400)
  11. turtle.penup()
  12. turtle.goto(-300, -50)
  13. turtle.pendown()
  14. turtle.pensize(2)
  15. koch(600, 3) # 0阶科赫曲线长度,阶数
  16. turtle.hideturtle()
  17. turtle.done()
  18. main()

image.png

  1. import turtle
  2. def koch(size, n):
  3. if n == 0:
  4. turtle.fd(size)
  5. else:
  6. for angle in [0, 60, -120, 60]:
  7. turtle.left(angle)
  8. koch(size / 3, n - 1)
  9. def main():
  10. turtle.setup(600, 600)
  11. turtle.penup()
  12. turtle.goto(-200, 100)
  13. turtle.pendown()
  14. turtle.pensize(2)
  15. level = 3 # 3阶科赫雪花,阶数
  16. koch(400, level)
  17. turtle.right(120)
  18. koch(400, level)
  19. turtle.right(120)
  20. koch(400, level)
  21. turtle.hideturtle()
  22. turtle.done()
  23. main()

image.png

基本统计值计算

  1. def getNum(): # 获取用户不定长度的输入
  2. nums = []
  3. iNumStr = input("请输入数字(回车退出): ")
  4. while iNumStr != "":
  5. nums.append(eval(iNumStr))
  6. iNumStr = input("请输入数字(回车退出): ")
  7. return nums
  8. def mean(numbers): # 计算平均值
  9. s = 0.0
  10. for num in numbers:
  11. s = s + num
  12. return s / len(numbers)
  13. def dev(numbers, mean): # 计算方差
  14. sdev = 0.0
  15. for num in numbers:
  16. sdev = sdev + (num - mean) ** 2
  17. return pow(sdev / (len(numbers) - 1), 0.5)
  18. def median(numbers): # 计算中位数
  19. sorted(numbers)
  20. size = len(numbers)
  21. if size % 2 == 0:
  22. med = (numbers[size // 2 - 1] + numbers[size // 2]) / 2
  23. else:
  24. med = numbers[size // 2]
  25. return med
  26. n = getNum() # 主体函数
  27. m = mean(n)
  28. print("平均值:{},方差:{:.2},中位数:{}.".format(m, dev(n, m), median(n)))
  29. """
  30. 请输入数字(回车退出): 1
  31. 请输入数字(回车退出): 2
  32. 请输入数字(回车退出): 3
  33. 请输入数字(回车退出): 4
  34. 请输入数字(回车退出): 5
  35. 请输入数字(回车退出):
  36. 平均值:3.0,方差:1.6,中位数:3.
  37. """

文本词频统计

  1. def getText():
  2. txt = open("hamlet.txt", "r").read()
  3. txt = txt.lower()
  4. for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_‘{|}~':
  5. txt = txt.replace(ch, " ") # 将文本中特殊字符替换为空格
  6. return txt
  7. hamletTxt = getText()
  8. words = hamletTxt.split()
  9. counts = {}
  10. for word in words:
  11. counts[word] = counts.get(word, 0) + 1
  12. items = list(counts.items())
  13. items.sort(key=lambda x: x[1], reverse=True)
  14. for i in range(10):
  15. word, count = items[i]
  16. print("{0:<10}{1:>5}".format(word, count))
  17. """
  18. the 1138
  19. and 965
  20. to 754
  21. of 669
  22. you 550
  23. i 542
  24. a 542
  25. my 514
  26. hamlet 462
  27. in 436
  28. """
  1. import jieba
  2. txt = open("threekingdoms.txt", "r", encoding="utf-8").read()
  3. words = jieba.lcut(txt)
  4. counts = {}
  5. for word in words:
  6. if len(word) == 1:
  7. continue
  8. else:
  9. counts[word] = counts.get(word, 0) + 1
  10. items = list(counts.items())
  11. items.sort(key=lambda x: x[1], reverse=True)
  12. for i in range(15):
  13. word, count = items[i]
  14. print("{0:<10}{1:>5}".format(word, count))
  15. """
  16. 曹操 953
  17. 孔明 836
  18. 将军 772
  19. 却说 656
  20. 玄德 585
  21. 关公 510
  22. 丞相 491
  23. 二人 469
  24. 不可 440
  25. 荆州 425
  26. 玄德曰 390
  27. 孔明曰 390
  28. 不能 384
  29. 如此 378
  30. 张飞 358
  31. """
  1. import jieba
  2. excludes = {"将军", "却说", "荆州", "二人", "不可", "不能", "如此"}
  3. txt = open("threekingdoms.txt", "r", encoding="utf-8").read()
  4. words = jieba.lcut(txt)
  5. counts = {}
  6. for word in words:
  7. if len(word) == 1:
  8. continue
  9. elif word == "诸葛亮" or word == "孔明曰":
  10. rword = "孔明"
  11. elif word == "关公" or word == "云长":
  12. rword = "关羽"
  13. elif word == "玄德" or word == "玄德曰":
  14. rword = "刘备"
  15. elif word == "孟德" or word == "丞相":
  16. rword = "曹操"
  17. else:
  18. rword = word
  19. counts[rword] = counts.get(rword, 0) + 1
  20. for word in excludes:
  21. del counts[word]
  22. items = list(counts.items())
  23. items.sort(key=lambda x: x[1], reverse=True)
  24. for i in range(10):
  25. word, count = items[i]
  26. print("{0:<10}{1:>5}".format(word, count))
  27. """
  28. 曹操 1451
  29. 孔明 1383
  30. 刘备 1252
  31. 关羽 784
  32. 张飞 358
  33. 商议 344
  34. 如何 338
  35. 主公 331
  36. 军士 317
  37. 吕布 300
  38. """

自动轨迹绘制

  1. import turtle as t
  2. t.title("自动轨迹绘制")
  3. t.setup(800, 600, 0, 0)
  4. t.pencolor("red")
  5. t.pensize(5)
  6. # 数据读取
  7. datals = []
  8. f = open("data.txt")
  9. for line in f:
  10. line = line.replace("\n", "")
  11. datals.append(list(map(eval, line.split(","))))
  12. f.close()
  13. # 自动绘制
  14. for i in range(len(datals)):
  15. t.pencolor(datals[i][3], datals[i][4], datals[i][5])
  16. t.fd(datals[i][0])
  17. if datals[i][1]:
  18. t.rt(datals[i][2])
  19. else:
  20. t.lt(datals[i][2])
  21. t.done()

image.png

政府工作报告词云

  1. import jieba
  2. import wordcloud
  3. f = open("新时代中国特色社会主义.txt", "r", encoding="utf-8")
  4. t = f.read()
  5. f.close()
  6. ls = jieba.lcut(t)
  7. txt = " ".join(ls)
  8. w = wordcloud.WordCloud(
  9. width=1000, height=700, background_color="white", font_path="msyh.ttc"
  10. )
  11. w.generate(txt)
  12. w.to_file("grwordcloud.png")

grwordcloud.png

  1. import jieba
  2. import wordcloud
  3. f = open("关于实施乡村振兴战略的意见.txt", "r", encoding="utf-8")
  4. t = f.read()
  5. f.close()
  6. ls = jieba.lcut(t)
  7. txt = " ".join(ls)
  8. w = wordcloud.WordCloud(
  9. width=1000, height=700, background_color="white", font_path="msyh.ttc"
  10. )
  11. w.generate(txt)
  12. w.to_file("grwordcloud.png")

grwordcloud.png

  1. import jieba
  2. import wordcloud
  3. f = open("新时代中国特色社会主义.txt", "r", encoding="utf-8")
  4. t = f.read()
  5. f.close()
  6. ls = jieba.lcut(t)
  7. txt = " ".join(ls)
  8. w = wordcloud.WordCloud(
  9. width=1000, height=700, background_color="white", font_path="msyh.ttc", max_words=15
  10. )
  11. w.generate(txt)
  12. w.to_file("grwordcloud.png")

grwordcloud.png

  1. import jieba
  2. import wordcloud
  3. f = open("关于实施乡村振兴战略的意见.txt", "r", encoding="utf-8")
  4. t = f.read()
  5. f.close()
  6. ls = jieba.lcut(t)
  7. txt = " ".join(ls)
  8. w = wordcloud.WordCloud(
  9. width=1000, height=700, background_color="white", font_path="msyh.ttc", max_words=15
  10. )
  11. w.generate(txt)
  12. w.to_file("grwordcloud.png")

grwordcloud.png

  1. import jieba
  2. import wordcloud
  3. from imageio import imread
  4. mask = imread("fivestart.png")
  5. excludes = {}
  6. f = open("新时代中国特色社会主义.txt", "r", encoding="utf-8")
  7. t = f.read()
  8. f.close()
  9. ls = jieba.lcut(t)
  10. txt = " ".join(ls)
  11. w = wordcloud.WordCloud(
  12. width=1000, height=700, background_color="white", font_path="msyh.ttc", mask=mask
  13. )
  14. w.generate(txt)
  15. w.to_file("grwordcloudm.png")

grwordcloudm.png

  1. import jieba
  2. import wordcloud
  3. from imageio import imread
  4. mask = imread("fivestart.png")
  5. excludes = {}
  6. f = open("关于实施乡村振兴战略的意见.txt", "r", encoding="utf-8")
  7. t = f.read()
  8. f.close()
  9. ls = jieba.lcut(t)
  10. txt = " ".join(ls)
  11. w = wordcloud.WordCloud(
  12. width=1000, height=700, background_color="white", font_path="msyh.ttc", mask=mask
  13. )
  14. w.generate(txt)
  15. w.to_file("grwordcloudm.png")

grwordcloudm.png

  1. import jieba
  2. import wordcloud
  3. from imageio import imread
  4. mask = imread("chinamap.jpg")
  5. excludes = {}
  6. f = open("新时代中国特色社会主义.txt", "r", encoding="utf-8")
  7. t = f.read()
  8. f.close()
  9. ls = jieba.lcut(t)
  10. txt = " ".join(ls)
  11. w = wordcloud.WordCloud(
  12. width=1000, height=700, background_color="white", font_path="msyh.ttc", mask=mask
  13. )
  14. w.generate(txt)
  15. w.to_file("grwordcloudm.png")

grwordcloudm.png

  1. import jieba
  2. import wordcloud
  3. from imageio import imread
  4. mask = imread("chinamap.jpg")
  5. excludes = {}
  6. f = open("关于实施乡村振兴战略的意见.txt", "r", encoding="utf-8")
  7. t = f.read()
  8. f.close()
  9. ls = jieba.lcut(t)
  10. txt = " ".join(ls)
  11. w = wordcloud.WordCloud(
  12. width=1000, height=700, background_color="white", font_path="msyh.ttc", mask=mask
  13. )
  14. w.generate(txt)
  15. w.to_file("grwordcloudm.png")

grwordcloudm.png

体育竞技分析

  1. from random import random
  2. def printIntro():
  3. print("这个程序模拟两个选手A和B的某种竞技比赛")
  4. print("程序运行需要A和B的能力值(以0到1之间的小数表示)")
  5. def getInputs():
  6. a = eval(input("请输入选手A的能力值(0-1): "))
  7. b = eval(input("请输入选手B的能力值(0-1): "))
  8. n = eval(input("模拟比赛的场次: "))
  9. return a, b, n
  10. def simNGames(n, probA, probB):
  11. winsA, winsB = 0, 0
  12. for i in range(n):
  13. scoreA, scoreB = simOneGame(probA, probB)
  14. if scoreA > scoreB:
  15. winsA += 1
  16. else:
  17. winsB += 1
  18. return winsA, winsB
  19. def gameOver(a, b):
  20. return a == 15 or b == 15
  21. def simOneGame(probA, probB):
  22. scoreA, scoreB = 0, 0
  23. serving = "A"
  24. while not gameOver(scoreA, scoreB):
  25. if serving == "A":
  26. if random() < probA:
  27. scoreA += 1
  28. else:
  29. serving = "B"
  30. else:
  31. if random() < probB:
  32. scoreB += 1
  33. else:
  34. serving = "A"
  35. return scoreA, scoreB
  36. def printSummary(winsA, winsB):
  37. n = winsA + winsB
  38. print("竞技分析开始,共模拟{}场比赛".format(n))
  39. print("选手A获胜{}场比赛,占比{:0.1%}".format(winsA, winsA / n))
  40. print("选手B获胜{}场比赛,占比{:0.1%}".format(winsB, winsB / n))
  41. def main():
  42. printIntro()
  43. probA, probB, n = getInputs()
  44. winsA, winsB = simNGames(n, probA, probB)
  45. printSummary(winsA, winsB)
  46. main()
  47. """
  48. 这个程序模拟两个选手A和B的某种竞技比赛
  49. 程序运行需要A和B的能力值(以0到1之间的小数表示)
  50. 请输入选手A的能力值(0-1): 0.5
  51. 请输入选手B的能力值(0-1): 0.5
  52. 模拟比赛的场次: 1000
  53. 竞技分析开始,共模拟1000场比赛
  54. 选手A获胜548场比赛,占比54.8%
  55. 选手B获胜452场比赛,占比45.2%
  56. 这个程序模拟两个选手A和B的某种竞技比赛
  57. 程序运行需要A和B的能力值(以0到1之间的小数表示)
  58. 请输入选手A的能力值(0-1): 0.5
  59. 请输入选手B的能力值(0-1): 0.5
  60. 模拟比赛的场次: 1000
  61. 竞技分析开始,共模拟1000场比赛
  62. 选手A获胜499场比赛,占比49.9%
  63. 选手B获胜501场比赛,占比50.1%
  64. """

第三方库安装脚本

  1. import os
  2. libs = {
  3. "numpy",
  4. "matplotlib",
  5. "pillow",
  6. "sklearn",
  7. "requests",
  8. "jieba",
  9. "beautifulsoup4",
  10. "wheel",
  11. "networkx",
  12. "sympy",
  13. "pyinstaller",
  14. "django",
  15. "flask",
  16. "werobot",
  17. "pyqt5",
  18. "pandas",
  19. "pyopengl",
  20. "pypdf2",
  21. "docopt",
  22. "pygame",
  23. }
  24. try:
  25. for lib in libs:
  26. os.system("pip3 install " + lib)
  27. print("Successful")
  28. except:
  29. print("Failed Somehow")

霍兰德人格分析雷达图

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import matplotlib
  4. matplotlib.rcParams["font.family"] = "Hiragino Sans GB"
  5. radar_labels = np.array(
  6. ["研究型(I)", "艺术型(A)", "社会型(S)", "企业型(E)", "常规型(C)", "现实型(R)", "研究型(I)"]
  7. ) # 雷达标签,增加一个起始元素以与 angles 数组长度匹配
  8. nAttr = 6
  9. data = np.array(
  10. [
  11. [0.40, 0.32, 0.35, 0.30, 0.30, 0.88],
  12. [0.85, 0.35, 0.30, 0.40, 0.40, 0.30],
  13. [0.43, 0.89, 0.30, 0.28, 0.22, 0.30],
  14. [0.30, 0.25, 0.48, 0.85, 0.45, 0.40],
  15. [0.20, 0.38, 0.87, 0.45, 0.32, 0.28],
  16. [0.34, 0.31, 0.38, 0.40, 0.92, 0.28],
  17. ]
  18. ) # 数据值
  19. data_labels = ("艺术家", "实验员", "工程师", "推销员", "社会工作者", "记事员")
  20. angles = np.linspace(0, 2 * np.pi, nAttr, endpoint=False)
  21. data = np.concatenate((data, [data[0]]))
  22. angles = np.concatenate((angles, [angles[0]]))
  23. fig = plt.figure(facecolor="white")
  24. plt.subplot(111, polar=True)
  25. plt.plot(angles, data, "o-", linewidth=1, alpha=0.2)
  26. plt.fill(angles, data, alpha=0.25)
  27. plt.thetagrids(angles * 180 / np.pi, radar_labels) # 移除 frac 参数
  28. plt.figtext(0.52, 0.95, "霍兰德人格分析", ha="center", size=20)
  29. legend = plt.legend(data_labels, loc=(0.94, 0.80), labelspacing=0.1)
  30. plt.setp(legend.get_texts(), fontsize="large")
  31. plt.grid(True)
  32. plt.savefig("holland_radar.jpg")
  33. plt.show()

image.png

玫瑰花绘制

  1. import turtle as t
  2. # 定义一个曲线绘制函数
  3. def DegreeCurve(n, r, d=1):
  4. for i in range(n):
  5. t.left(d)
  6. t.circle(r, abs(d))
  7. # 初始位置设定
  8. s = 0.2 # size
  9. t.setup(450 * 5 * s, 750 * 5 * s)
  10. t.pencolor("black")
  11. t.fillcolor("red")
  12. t.speed(100)
  13. t.penup()
  14. t.goto(0, 900 * s)
  15. t.pendown()
  16. # 绘制花朵形状
  17. t.begin_fill()
  18. t.circle(200 * s, 30)
  19. DegreeCurve(60, 50 * s)
  20. t.circle(200 * s, 30)
  21. DegreeCurve(4, 100 * s)
  22. t.circle(200 * s, 50)
  23. DegreeCurve(50, 50 * s)
  24. t.circle(350 * s, 65)
  25. DegreeCurve(40, 70 * s)
  26. t.circle(150 * s, 50)
  27. DegreeCurve(20, 50 * s, -1)
  28. t.circle(400 * s, 60)
  29. DegreeCurve(18, 50 * s)
  30. t.fd(250 * s)
  31. t.right(150)
  32. t.circle(-500 * s, 12)
  33. t.left(140)
  34. t.circle(550 * s, 110)
  35. t.left(27)
  36. t.circle(650 * s, 100)
  37. t.left(130)
  38. t.circle(-300 * s, 20)
  39. t.right(123)
  40. t.circle(220 * s, 57)
  41. t.end_fill()
  42. # 绘制花枝形状
  43. t.left(120)
  44. t.fd(280 * s)
  45. t.left(115)
  46. t.circle(300 * s, 33)
  47. t.left(180)
  48. t.circle(-300 * s, 33)
  49. DegreeCurve(70, 225 * s, -1)
  50. t.circle(350 * s, 104)
  51. t.left(90)
  52. t.circle(200 * s, 105)
  53. t.circle(-500 * s, 63)
  54. t.penup()
  55. t.goto(170 * s, -30 * s)
  56. t.pendown()
  57. t.left(160)
  58. DegreeCurve(20, 2500 * s)
  59. DegreeCurve(220, 250 * s, -1)
  60. # 绘制一个绿色叶子
  61. t.fillcolor("green")
  62. t.penup()
  63. t.goto(670 * s, -180 * s)
  64. t.pendown()
  65. t.right(140)
  66. t.begin_fill()
  67. t.circle(300 * s, 120)
  68. t.left(60)
  69. t.circle(300 * s, 120)
  70. t.end_fill()
  71. t.penup()
  72. t.goto(180 * s, -550 * s)
  73. t.pendown()
  74. t.right(85)
  75. t.circle(600 * s, 40)
  76. # 绘制另一个绿色叶子
  77. t.penup()
  78. t.goto(-150 * s, -1000 * s)
  79. t.pendown()
  80. t.begin_fill()
  81. t.rt(120)
  82. t.circle(300 * s, 115)
  83. t.left(75)
  84. t.circle(300 * s, 100)
  85. t.end_fill()
  86. t.penup()
  87. t.goto(430 * s, -1070 * s)
  88. t.pendown()
  89. t.right(30)
  90. t.circle(-600 * s, 35)
  91. t.done()

image.png