《 Python程序设计(第3版)》[美] 约翰·策勒(John Zelle) 第 9 章 答案

您所在的位置:网站首页 python程序设计第三章答案 《 Python程序设计(第3版)》[美] 约翰·策勒(John Zelle) 第 9 章 答案

《 Python程序设计(第3版)》[美] 约翰·策勒(John Zelle) 第 9 章 答案

2024-07-13 17:15| 来源: 网络整理| 查看: 265

True/False FalseFalseTrueTrueTrueFalseTrueFalseFalseFalse

Multiple Choice bcdacabaab Discussion randrange(11)random() - 0.5randrange(1,7)randrange(1,7) + randrange(1,7)random()*20 - 10.0The issue here is mainly comfort/risk. When there is uncertainty whether the problem can even be solved, starting with an experimentatl prototype is probably a good idea. Programming Exercises

1.

解法一(绝对正确)

# c09ex01.py # Racquetball best of n games from random import random def main(): printIntro() probA, probB, n, matches = getInputs() winsA, winsB = simMatches(matches, n, probA, probB) printSummary(winsA, winsB) def printIntro(): # Prints an introduction to the program print("This program simulates racquetball matches between two") print('players called "A" and "B". The abilities of each player is') print("indicated by a probability (a number between 0 and 1) that") print("the player wins the point when serving. Player A always") print("serves first in the first game of a match, and the first") print("service alternates in subsequent games. \n") def getInputs(): # RETURNS probA, probB, number of games to simulate a = float(input("What is the prob. player A wins a serve? ")) b = float(input("What is the prob. player B wins a serve? ")) n = int(input("How many games are in a match? ")) m = int(input("How many matches should be simulated? ")) return a, b, n, m def simMatches(howMany, n, probA, probB): # Simulates n games of racquetball between players A and B # RETURNS number of wins for A, number of wins for B winsA = winsB = 0 for i in range(howMany): gamesA, gamesB = simOneMatch(n, probA, probB) if gamesA > gamesB: winsA = winsA + 1 else: winsB = winsB + 1 return winsA, winsB def simOneMatch(n, probA, probB): gamesA = gamesB = 0 needed = n/2 + 1 while gamesA < needed and gamesB < needed: # Alternate serves, A serves first game of match if (gamesA + gamesB) % 2 == 1: firstServe = "A" else: firstServe = "B" scoreA, scoreB = simOneGame(probA, probB, firstServe) if scoreA > scoreB: gamesA = gamesA + 1 else: gamesB = gamesB + 1 return gamesA, gamesB def simOneGame(probA, probB, Server): # Simulates a single game or racquetball between players A and B # RETURNS A's final score, B's final score serving = Server scoreA = 0 scoreB = 0 while not gameOver(scoreA, scoreB): if serving == "A": if random() < probA: scoreA = scoreA + 1 else: serving = "B" else: if random() < probB: scoreB = scoreB + 1 else: serving = "A" return scoreA, scoreB def gameOver(a,b): # a and b are scores for players in a racquetball game # RETURNS true if game is over, false otherwise return a == 15 or b == 15 def printSummary(winsA, winsB): # Prints a summary of wins for each player. n = winsA + winsB print("\nMatches simulated:", n) print("Wins for A: {0} ({1:0.1%})".format(winsA, winsA/n)) print("Wins for B: {0} ({1:0.1%})".format(winsB, winsB/n)) if __name__ == "__main__": main()

解法二(不知道对不对)

from random import * def main(): printIntro() probA,probB,n = getInputs() winsA,winsB = simNGames(n,probA,probB) printSummary(winsA,winsB) def printIntro(): print('This program simulates a game of racquetball between two ') print('players called "A" and "B". The ability of each player is') print('indicated by a probability (a number between 0 and 1) that') print("the player wins the point when serving. Player A always") print('has the first serve.') def getInputs(): a = float(input("What is the prob.player A wins a serve? ")) b = float(input("What is the prob.player B wins a serve? ")) n = int(input("How many games to simulate?")) return a,b,n def simNGames(n,probA,probB): winsA = winsB = 0 for i in range(n): scoreA,scoreB = simOneGame(probA,probB,i) if scoreA > scoreB:winsA = winsA + 1 if scoreB > scoreA:winsB = winsB + 1 return winsA,winsB def simOneGame(probA,probB,i): scoreA,scoreB = 0,0 if i % 2 == 0:serving = 'B' else:serving = 'A' while not gameOver(scoreA,scoreB): if serving == 'A': if random() < probA: scoreA = scoreA + 1 else:serving = 'B' else: if random() < probB:scoreB = scoreB + 1 else:serving = 'A' return scoreA,scoreB def gameOver(a,b): return a==15 or b == 15 def printSummary(winsA,winsB): n = winsA + winsB print('\nGames simulated:',n) print('Wins for A: {0} ({1:0.2%})'.format(winsA,winsA/n)) print('Wins for B: {0} ({1:0.2%})'.format(winsB,winsB/n)) if __name__ =='__main__':main()

2.

解法一

from random import * def main(): printIntro() probA,probB,n = getInputs() winsA,winsB,a0,b0 = simNGames(n,probA,probB) printSummary(winsA,winsB,a0,b0) def printIntro(): print('This program simulates a game of racquetball between two ') print('players called "A" and "B". The ability of each player is') print('indicated by a probability (a number between 0 and 1) that') print("the player wins the point when serving. Player A always") print('has the first serve.') def getInputs(): a = float(input("What is the prob.player A wins a serve? ")) b = float(input("What is the prob.player B wins a serve? ")) n = int(input("How many games to simulate?")) return a,b,n def simNGames(n,probA,probB): winsA = winsB = 0 a0 = b0 =0 for i in range(n): scoreA,scoreB = simOneGame(probA,probB) if scoreA > scoreB:winsA = winsA + 1 if scoreB > scoreA:winsB = winsB + 1 if scoreA == 0:b0 = b0 + 1 if scoreB == 0:a0 = a0 + 1 return winsA,winsB,a0,b0 def simOneGame(probA,probB): serving,scoreA,scoreB = 'A',0,0 while not gameOver(scoreA,scoreB): if serving == 'A': if random() < probA: scoreA = scoreA + 1 else:serving = 'B' else: if random() < probB:scoreB = scoreB + 1 else:serving = 'A' return scoreA,scoreB def gameOver(a,b): return a == 15 or b == 15 or (a == 7 and b ==0) or (a == 0 and b == 7) def printSummary(winsA,winsB,a0,b0): n = winsA + winsB print('\nGames simulated:',n) print('Wins for A: {0} ({1:0.2%})'.format(winsA,winsA/n),end =' ') print('Shutouts for A: {}({:.2%})'.format(a0,a0/winsA)) print('Wins for B: {0} ({1:0.2%})'.format(winsB,winsB/n),end =' ') print('Shutouts for B: {}({:.2%})'.format(b0,b0/winsB)) if __name__ =='__main__':main()

解法二 

from random import random def main(): printIntro() probA, probB, n = getInputs() winsA, shutsA, winsB, shutsB = simNGames(n, probA, probB) printSummary(winsA, shutsA, winsB, shutsB) def printIntro(): # Prints an introduction to the program print("This program simulates a game of racquetball between two") print('players called "A" and "B". The abilities of each player is') print("indicated by a probability (a number between 0 and 1) that") print("the player wins the point when serving. Player A always") print("has the first serve.\n") def getInputs(): # RETURNS probA, probB, number of games to simulate a = float(input("What is the prob. player A wins a serve? ")) b = float(input("What is the prob. player B wins a serve? ")) n = int(input("How many games to simulate? ")) return a, b, n def simNGames(n, probA, probB): # Simulates n games of racquetball between players A and B # RETURNS number of wins for A, number of wins for B winsA = winsB = 0 shutsA = shutsB = 0 for i in range(n): scoreA, scoreB = simOneGame(probA, probB) if scoreA > scoreB: winsA = winsA + 1 if scoreB == 0: shutsA = shutsA + 1 else: winsB = winsB + 1 if scoreA == 0: shutsB = shutsB + 1 return winsA, shutsA, winsB, shutsB def simOneGame(probA, probB): # Simulates a single game or racquetball between players A and B # RETURNS A's final score, B's final score serving = "A" scoreA = 0 scoreB = 0 while not gameOver(scoreA, scoreB): if serving == "A": if random() < probA: scoreA = scoreA + 1 else: serving = "B" else: if random() < probB: scoreB = scoreB + 1 else: serving = "A" return scoreA, scoreB def gameOver(a,b): # a and b are scores for players in a racquetball game # RETURNS true if game is over, false otherwise return a == 15 or b == 15 \ or (a == 7 and b == 0) \ or (b == 7 and a == 0) def printSummary(winsA, shutsA, winsB, shutsB): # Print a nicely formatted report n = winsA + winsB print("Summary of", n , "games:") print() print(" wins (% total) shutouts (% wins) ") print("--------------------------------------------") printLine("A", winsA, shutsA, n) printLine("B", winsB, shutsB, n) def printLine(label, wins, shuts, n): template = "Player {0}: {1:4} ({2:6.1%}) {3:11} ({4})" if wins == 0: # Avoid division by zero! shutStr = "------" else: shutStr = "{0:6.1%}".format(shuts/wins) print(template.format(label, wins, wins/n, shuts, shutStr)) if __name__ == "__main__": main()

3 正确性有待验证,正确的关键在gameResult(),oneGameResult()函数存在某种缺陷,导致不能实现比赛规则

from random import * def main(): print('This is a python program simulating the volleyball games.') n,pa,pb = getinput() wa,wb = gameResult(n,pa,pb) result(n,wa,wb) def getinput(): n = int(input("Enter the times to be simulate: ")) pa = float(input("Enter the winning probability of team A: ")) pb = float(input("Enter the winning probability of team B: ")) return n,pa,pb def finCondition(sa,sb): if sa > 14 and sb > 14 and abs(sa - sb) > 1: return True else:return False def oneGameResult(pa,pb): serving = 'A' if serving == "A": if random() < pa: sa += 1 else:serving = 'B' if serving == "B": if random() < pb: sb += 1 else:serving = 'A' def gameResult(n,pa,pb): wa = wb =0 for i in range(n): sa = sb = 0 while not finCondition(sa,sb): serving = 'A' if serving == "A": if random() < pa: sa += 1 else:serving = 'B' if serving == "B": if random() < pb: sb += 1 else:serving = 'A' if sa > sb:wa += 1 if sa < sb:wb += 1 return wa,wb def result(n,wa,wb): print("Games simulated: {}".format(n)) print("Wins for A: {} Winning percentage for A: {:.2%}".format(wa,wa/n)) print("Wins for B: {} Winning percentage for B: {:.2%}".format(wb,wb/n)) if __name__ == '__main__': main()

4 正确性有待验证,oneGameResult()函数存在某种缺陷,导致不能实现比赛规则

from random import * def main(): print('This is a python program simulating the volleyball games.') n,pa,pb = getinput() wa,wb = gameResult(n,pa,pb) result(n,wa,wb) def getinput(): n = int(input("Enter the times to be simulate: ")) pa = float(input("Enter the shooting accuracy of team A: ")) pb = float(input("Enter the shooting accuracy of team B: ")) return n,pa,pb def finCondition(sa,sb): if sa == 25 or sb == 25: return True else:return False def oneGameResult(pa,pb): serving = 'A' if serving == "A": if random() < pa: sa += 1 else:serving = 'B' if serving == "B": if random() < pb: sb += 1 else:serving = 'A' def gameResult(n,pa,pb): wa = wb =0 for i in range(n): sa = sb = 0 while not finCondition(sa,sb): if i % 2 ==0:serving = 'A' else:serving = "B" if serving == "A": if random() > pb: sa += 1 else: if random() > pa: sb += 1 serving = 'B' if serving == "B": if random() < pa: sb += 1 else: if random() > pa: sb += 1 serving = 'B' if sa > sb:wa += 1 if sa < sb:wb += 1 return wa,wb def result(n,wa,wb): print("Games simulated: {}".format(n)) print("Wins for A: {} Winning percentage for A: {:.2%}".format(wa,wa/n)) print("Wins for B: {} Winning percentage for B: {:.2%}".format(wb,wb/n)) if __name__ == '__main__': main()

7

解法一

from random import * def main(): n = int(input("Enter the times of the game to simulate: ")) wins = simGame(n) print("Wins: {}\nWinning percentage: {:.2%}".format(wins,wins/n)) def roll(): r = randrange(1,7) + randrange(1,7) return r def simGame(n): wins = losses = 0 for i in range(n): r1 = roll() if r1 in [2,3,12]:losses += 1 elif r1 in [7,11]:wins += 1 else: wins += reRoll(r1) return wins def reRoll(r): while True: R = roll() if R == 7: return 0 break elif R == r: return 1 break if __name__ =="__main__":main()

解法二(绝对正确)

# c09ex07.py # Simulation of game of craps. from random import randrange def main(): print("This program estimates the probability of winning at craps.") n = int(input("How many games should I simulate? ")) wins = simNGames(n) print("\nThe player wins", wins, "games.") print("The estimated probabillity of a win is {0:0.2%}".format(wins/n)) def simNGames(n): wins = 0 for i in range(n): if winCraps(): wins = wins + 1 return wins def winCraps(): roll = rollDice() if roll == 7 or roll == 11: return True elif roll == 2 or roll == 3 or roll == 12: return False else: return rollForPoint(roll) def rollForPoint(toMake): roll = rollDice() while roll != 7 and roll != toMake: roll = rollDice() return roll == toMake def rollDice(): return randrange(1,7) + randrange(1,7) if __name__ == '__main__': main()

8

来看一种错误的解法(预测概率37.4%)。你能看出哪里错误吗?在评论区有奖竞答,欢迎读者朋友留言。答对的朋友可以获得python书籍。

from random import * def main(): print("Simulation of a Blackjack dealer.\n") n = int(input("How many trials? ")) busts = 0 for i in range(n): points,hasA = 0,False while points < 17: r = randrange(1,14) if r > 10:r = 10 points,hasA = add(points,r,hasA) if points > 21: busts += 1 print('Games simulated: {0}'.format(n)) print("Bust times: {}".format(busts)) print("Estimated bust prob: {:.2%}".format(busts/n)) def add(points,r,hasA): if r != 1: if hasA == False: return points + r,hasA else: if points + 10 + r > 16: return points + 10 + r,hasA else: return points + r,hasA if r == 1: hasA = True if 16 < points + r + 10: return points,hasA else: return points + r,hasA if __name__ == '__main__':main()

正确解法(预测概率28.17%)

# c09ex08.py # Simulation to Blackjack dealer to estimate bust probability. from random import randrange def main(): print("Simulation of a Blackjack dealer.\n") n = int(input("How many trials? ")) busts = 0 for i in range(n): points = dealHand() if points > 21: busts = busts + 1 print("In %d hands dealer busted %d times." % (n, busts)) print("Estimated prob =", float(busts)/n) def dealHand(): total = 0 haveAce = False while total < 17: card = randrange(1,14) if card == 1: haveAce = True total = total + BJValue(card) if haveAce: total = adjustForAce(total) return total def BJValue(card): if card > 10: return 10 else: return card def adjustForAce(total): if 16 < total + 10 < 22: return total + 10 else: return total if __name__ == '__main__': main()

9

# c09ex08.py # Simulation to Blackjack dealer to estimate bust probability. from random import randrange def main(): print("Simulation of a Blackjack dealer.\n") n = int(input("How many trials for each start value? ")) print("\nStart Prob of Bust") print("-------------------") for start in range(1,11): busts = simulateHands(n, start) print("{0:5} {1:7.2f}".format(start, busts/n)) def simulateHands(n, start): busts = 0 for i in range(n): points = dealHand(start) if points > 21: busts = busts + 1 return busts def dealHand(start): total = start haveAce = (start == 1) while total < 17: card = randrange(1,14) if card == 1: haveAce = True total = total + BJValue(card) if haveAce: total = adjustForAce(total) return total def BJValue(card): if card > 10: return 10 else: return card def adjustForAce(total): if 16 < total + 10 < 22: return total + 10 else: return total if __name__ == '__main__': main()

10

解法一

from random import * import math def main(): n,h = int(input("Enter the times to simulate: ")),0 for i in range(n): x,y = 2*random() - 1,2*random() - 1 h += isCircle(x,y) pi = 4*h/n print("Estimated times:",n) print("Estimated pi:",pi) print("Accuarate pi:",math.pi) print("Error",abs(pi - math.pi)) def isCircle(x,y): if x*x + y*y


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3