Files
Python-Programming-Exercise/20210801-第9章.py
2025-09-25 21:23:34 +08:00

293 lines
11 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# ==============================================================================
# 通过 D:\文档\电子文档\个人\脚本库\PythonScript\第9章.py + 题号 运行
# 程序名:
# 第9章习题
# 目的:
#
# 修订记录:
# 日期 编程者 改动描述
# =================== ============= =====================================
# 2021-08-01 19:20:17 Sola 9-a 模拟短柄壁球结果
# 2021-08-01 20:12:22 Sola 9-1 模拟短柄壁球改进
# 2021-08-01 20:16:29 Sola 9-2 不知道零封是什么规则
# 2021-08-01 20:16:50 Sola 9-3 没看懂题目说的规则是什么
# 2021-08-01 20:17:04 Sola 9-4 现代排球比赛
# 2021-08-01 20:20:56 Sola 9-5 略过
# 2021-08-01 20:21:04 Sola 9-6 略
# 2021-08-01 20:21:18 Sola 9-7 花旗骰
# 2021-08-01 21:07:43 Sola 9-8 21点发牌者爆牌可能性
# 2021-08-02 00:52:36 Sola 9-9 21点发牌者各首牌爆牌可能性
# 2021-08-23 20:04:36 Sola 9-10 蒙特卡罗估计PI的值
def Test9_a():
# 击球失误的选手输掉这一回合
# 如果输家是发球选手,则发球权转给另一名选手
# 如果发球选手赢得了这一回合,则会得 1 分
# 选手只能在自己发球时得分
# 第一名得到 15 分的选手赢得比赛
from random import random
winProbA = float(input("What is the prob. player A wins a serve? "))
winProbB = float(input("What is the prob. player B wins a serve? "))
times = int(input("How many games to simulate? "))
winsA, winsB = 0, 0
for i in range(times):
scoreA, scoreB = 0, 0
servingSide = "A"
while scoreA < 15 and scoreB < 15:
if servingSide == "A":
if random() <= winProbA:
scoreA = scoreA + 1
else:
servingSide = "B"
else:
if random() <= winProbB:
scoreB = scoreB + 1
else:
servingSide = "A"
if scoreA > scoreB:
winsA = winsA + 1
else:
winsB = winsB + 1
print("\nGames Simulated: {0}".format(times) +
"\nWins for A: {0:3} ({1:4.1%})".format(winsA, winsA / times) +
"\nWins for A: {0:3} ({1:4.1%})".format(winsB, winsB / times))
def Test9_1():
from random import random
winProbA = float(input("What is the prob. player A wins a serve? "))
winProbB = float(input("What is the prob. player B wins a serve? "))
times = int(input("How many games to simulate? "))
winsA, winsB = 0, 0
for i in range(times):
scoreA, scoreB = 0, 0
if times % 2 != 0:
servingSide = "A"
else:
servingSide = "B"
while scoreA < 15 and scoreB < 15:
if servingSide == "A":
if random() <= winProbA:
scoreA = scoreA + 1
else:
servingSide = "B"
else:
if random() <= winProbB:
scoreB = scoreB + 1
else:
servingSide = "A"
if scoreA > scoreB:
winsA = winsA + 1
else:
winsB = winsB + 1
print("\nGames Simulated: {0}".format(times) +
"\nWins for A: {0:3} ({1:4.1%})".format(winsA, winsA / times) +
"\nWins for A: {0:3} ({1:4.1%})".format(winsB, winsB / times))
def Test9_4():
from random import random
winProbA = float(input("What is the prob. player A wins a serve? "))
winProbB = float(input("What is the prob. player B wins a serve? "))
times = int(input("How many games to simulate? "))
winsA, winsB = 0, 0
for i in range(times):
scoreA, scoreB = 0, 0
if times % 2 != 0:
servingSide = "A"
else:
servingSide = "B"
while scoreA < 25 and scoreB < 25:
if servingSide == "A":
if random() <= winProbA:
scoreA = scoreA + 1
else:
servingSide = "B"
scoreB = scoreB + 1
else:
if random() <= winProbB:
scoreB = scoreB + 1
else:
servingSide = "A"
scoreA = scoreA + 1
if scoreA > scoreB:
winsA = winsA + 1
else:
winsB = winsB + 1
print("\nGames Simulated: {0}".format(times) +
"\nWins for A: {0:3} ({1:4.1%})".format(winsA, winsA / times) +
"\nWins for A: {0:3} ({1:4.1%})".format(winsB, winsB / times))
def Test9_7():
def main():
introduce()
times = getValue()
wins = simNGame(times)
printResult(wins, times)
def introduce():
print("这是一个用来模拟花旗骰的程序:" +
"\n游戏规则如下:" +
"\n1. 一个玩家掷一双普通的六面骰子" +
"\n 1.1 如果初始点数是 2、3 或 12则玩家失败" +
"\n 1.2 如果是 7 或 11则玩家获胜" +
"\n 1.3 任何其他初始点数将导致玩家“再掷点”" +
"\n2. 第一轮无结果,则玩家持续掷骰子直到掷出 7 或重新掷出初始点" +
"\n 2.1 如果选手在掷出 7 之前重新掷出初始点,就获胜" +
"\n 2.2 先掷出 7 则失败")
def getValue():
times = int(input("请输入模拟的次数:"))
return times
def simNGame(times):
wins = 0
for i in range(times):
wins = wins + result()
return wins
def printResult(wins, times):
print("\n你的模拟获胜次数为:{0:3} ({1:4.1%})".format(wins, wins / times))
def result():
firstPoint = getPoint()
if firstPoint == 2 or firstPoint == 3 or firstPoint == 12:
score = 0
elif firstPoint == 7 or firstPoint == 11:
score = 1
else:
while True:
thisPoint = getPoint()
if thisPoint == 7:
score = 0
break
elif thisPoint == firstPoint:
score = 1
break
return score
def getPoint():
from random import randrange
return randrange(1, 7) + randrange(1, 7)
main()
def Test9_8():
def main():
introduce()
times = getValue()
booms = simNGame(times)
printResult(booms, times)
def introduce():
print("二十一点是用纸牌玩的赌场游戏。游戏的目标是拿到尽可能接近 21 点的牌,但不超过。" +
"所有花牌为 10 分A 为 1 或 11所有其他牌均按值计分" +
"\n该游戏是针对发牌者进行的。玩家尝试比发牌者更接近 21 点(不超过)" +
"\n1. 如果发牌者爆牌(超过 21玩家自动获胜只要玩家尚未爆牌" +
"\n2. 发牌者必须始终根据固定的规则取牌" +
"\n3. 发牌者至少发牌直到自己达到 17 点以上" +
"\n4. 如果发牌者的牌中包含一个 A那么如果总和在 1721 之间时(含 21" +
",它将被计为 11" +
"\n5. 否则A 被计为 1")
def getValue():
times = int(input("请输入模拟的次数:"))
return times
def simNGame(times):
booms = 0
for i in range(times):
booms = booms + result()
return booms
def printResult(booms, times):
print("\n模拟发牌者爆牌次数为:{0:3} ({1:4.1%})".format(booms, booms / times))
def result():
from random import randrange
sumA, sumB = 0, 0
hasA, hasB = 0, 0
while True:
cardA = randrange(1, 12)
if cardA == 1:
hasA = hasA + 1
sumA = sumA + min(10, cardA)
cardB = randrange(1, 12)
if cardB == 1:
hasB = hasB + 1
sumB = sumB + min(10, cardB)
if 21 >= sumA + 10 * hasA >= 17 or sumA > 21 or sumB > 21 or (sumA >= 17 and hasA > 0):
break
if sumA > 17 and hasA > 0 and sumB <= 21:
return 1
elif sumA > 21 and sumB <= 21:
return 1
else:
return 0
main()
def Test9_9():
def main():
introduce()
times = getValue()
for j in range(1, 11):
booms = simNGame(times, j)
printResult(booms, times, j)
def introduce():
print("二十一点是用纸牌玩的赌场游戏。游戏的目标是拿到尽可能接近 21 点的牌,但不超过。" +
"所有花牌为 10 分A 为 1 或 11所有其他牌均按值计分" +
"\n该游戏是针对发牌者进行的。玩家尝试比发牌者更接近 21 点(不超过)" +
"\n1. 如果发牌者爆牌(超过 21玩家自动获胜只要玩家尚未爆牌" +
"\n2. 发牌者必须始终根据固定的规则取牌" +
"\n3. 发牌者至少发牌直到自己达到 17 点以上" +
"\n4. 如果发牌者的牌中包含一个 A那么如果总和在 1721 之间时(含 21" +
",它将被计为 11" +
"\n5. 否则A 被计为 1")
def getValue():
times = int(input("请输入每个首牌模拟的次数:"))
return times
def simNGame(times, j):
booms = 0
for i in range(times):
booms = booms + result(j)
return booms
def printResult(booms, times, card):
print("首牌为{2:2}时,模拟发牌者爆牌次数为:{0:5} ({1:4.1%})".format(booms, booms / times, card))
def result(j):
from random import randrange
sumA, sumB = 0, 0
hasA, hasB = 0, 0
cardA = j
if cardA == 1:
hasA = hasA + 1
sumA = sumA + min(10, cardA)
cardB = randrange(1, 12)
if cardB == 1:
hasB = hasB + 1
sumB = sumB + min(10, cardB)
while True:
cardA = randrange(1, 12)
if cardA == 1:
hasA = hasA + 1
sumA = sumA + min(10, cardA)
cardB = randrange(1, 12)
if cardB == 1:
hasB = hasB + 1
sumB = sumB + min(10, cardB)
if 21 >= sumA + 10 * hasA >= 17 or sumA > 21 or sumB > 21 or (sumA >= 17 and hasA > 0):
break
if sumA > 17 and hasA > 0 and sumB <= 21:
return 1
elif sumA > 21 and sumB <= 21:
return 1
else:
return 0
main()
def Test9_10():
def main():
introduce()
times = getInput()
PIvalue = coculate(times)
printResult(PIvalue)
def introduce():
print("该程序接受飞镖数作为输入然后进行模拟估计PI")
def getInput():
times = int(input("请输入飞镖数:"))
return times
def coculate(times):
sum = 0
for i in range(times):
import sys
def Run(num):
eval("Test9_" + num + "()")
Run(sys.argv[1])