python - 如何创建一个输赢字典来跟踪赢得或输掉一场比赛所需的掷骰数?
问题描述
我是 python 编码的新手。这是一个掷骰子游戏。它播放了 50 次,我能够输出胜负,我还能够跟踪滚动计数。我想制作一本字典,记录赢得游戏和输掉游戏所需的掷骰数(一胜一松字典)。例如,这将是项目的键和值。4 : 20 意味着当游戏玩了 50 次时,需要 4 次掷骰才能赢或输(取决于是赢字典还是输字典)。这就是我到目前为止所拥有的。我渴望学习,但我被困在这里。我将不胜感激任何帮助。
import random
from collections import Counter
def roll_dice():
"""Roll two dice and return their face values as a tuple."""
die1 = random.randrange(1, 7)
die2 = random.randrange(1, 7)
return (die1, die2) # pack die face values into a tuple
def display_dice(dice):
"""Display one roll of the two dice."""
die1, die2 = dice # unpack the tuple into variables die1 and die2
print(f'Player rolled {die1} + {die2} = {sum(dice)}')
def clear_values():
"""Clear rollcount """
gamecount = 0
wincount = 0
losecount = 0
rollcount = 0
list_of_win = {}
list_of_loss = {}
win_roll = {1:0, 2 : 0, 3 : 0, 4 : 0, 5 : 0, 6 : 0, 7 : 0, 8 : 0, 9 : 0, 10 : 0, 11 : 0, 12 : 0, 13 : 0, 14 : 0, 15 : 0, 16 : 0, 17 : 0, 18 : 0, 19 : 0}
lose_roll = {1:0, 2 : 0, 3 : 0, 4 : 0, 5 : 0, 6 : 0, 7 : 0, 8 : 0, 9 : 0, 10 : 0, 11 : 0, 12 : 0, 13 : 0, 14 : 0, 15 : 0, 16 : 0, 17 : 0, 18 : 0, 19 : 0}
while gamecount != 50:
rollcount = 0
die_values = roll_dice()
rollcount += 1 # first roll
display_dice(die_values)
# determine game status and point, based on first roll
sum_of_dice = sum(die_values)
if sum_of_dice in (7, 11): # win
wincount += 1
gamecount += 1
list_of_win = [{rollcount : key, "nbr": value} for key, value in Counter(list_of_win).values()]
game_status = 'WON'
elif sum_of_dice in (2, 3, 12): # lose
losecount += 1
list_of_loss =[{rollcount : key, "nbr": value} for key, value in Counter(list_of_loss).values()]
gamecount += 1
game_status = 'LOST'
else: # remember point
game_status = 'CONTINUE'
my_point = sum_of_dice
print('Point is', my_point)
# continue rolling until player wins or loses
while game_status == 'CONTINUE':
die_values = roll_dice()
rollcount += 1
display_dice(die_values)
sum_of_dice = sum(die_values)
if sum_of_dice == my_point: # win by making point
game_status = 'WON'
wincount += 1
list_of_win =[{rollcount : key, "nbr": value} for key, value in Counter(list_of_win).values()]
gamecount += 1
elif sum_of_dice == 7: # lose by rolling 7
game_status = 'LOST'
losecount += 1
list_of_loss =[{rollcount : key, "nbr": value} for key, value in Counter(list_of_loss).values()]
gamecount += 1
# display "wins" or "loses" message
if game_status == 'WON':
print('Player wins')
else:
print('Player loses')
print(f'| {wincount} = wins |\n| {losecount} = losses |\n| {gamecount} = games played |')
解决方案
您在正确的轨道上,但不需要 Counter 对象。
为了解释,让我们只记录胜利。您将能够类似地跟踪损失。
除了分配 list_of_wins 的位置之外,您还可以增加存储在 win_rolls 中的计数器。您已经将字典中该 rollcount 的值初始化为 0,因此我们只需添加 1。
win_roll[rollcount] += 1
您必须在分配 list_of_win 的两个地方执行此操作。
然后在程序结束时,您可以打印出字典中的所有键和值。
for key, value in win_roll.items():
print(f'{key} : {value}')
当然,有可能需要超过 19 次才能赢得一场比赛,在这种情况下,此代码将不起作用。举个例子,它需要 30 次掷骰才能赢或输(不太可能,但在统计上仍然是可能的)。由于您尚未在字典中为键 30 初始化条目,因此不会有一个值可以增加。
在这种情况下,我们可以检查字典中是否存在键并采取适当的行动。代替
win_roll[rollcount] += 1
我们用
if rollcount in win_roll:
win_roll[rollcount] += 1
else:
win_roll[rollcount] = 1
您可以对 loss_roll 执行相同的操作,以跟踪每个 rollcount 赢或输的次数。
推荐阅读
- javascript - 浏览器:向使用自签名证书的 API 发出 HTTPS GET 请求。以编程方式,无需在用户端进行任何工作
- r - 在我的应用函数中使用 dplyr 的 slice_sample() 的正确方法是什么?
- apache-spark - spark-submit 将包添加到工作节点
- list - 如何使用 shared_preferences 包保存我的字符串列表?
- visual-studio-code - 必须按 ctrl + 单击并滚动鼠标滚轮才能在 vscode 中放大和缩小
- css - 如何在带有 React 和 Typescript 的样式组件中使用 @font-face?
- python - 计算pyspark中每个起点到目的地的最小距离
- kubernetes - 在主节点上创建 pod 时出错:错误无法获取沙盒容器任务:未找到任务
- javascript - 每获得一千个数字
- c++ - 如何修复 Visual Studio C++ 上的“找不到 libopenblas.dll”系统错误?