【发布时间】:2021-04-06 04:21:25
【问题描述】:
我正在开发掷骰子游戏,它会在两个骰子显示相同值之前打印出尝试次数 (n)。 我还想打印出过去的滚动结果。但是,我的代码只显示了最后的滚动结果(n-1 次尝试)。
我尝试用谷歌搜索并检查 stackoverflow 骰子滚动查询的过去历史,但我仍然无法弄清楚如何解决代码。请帮忙,我认为这与嵌套列表或字典有关,但我就是想不通。
下面是我的代码:
from random import randint
stop = 0
count = 0
record = []
while stop == 0:
roll = [(dice1, dice2) for i in range(count)]
dice1 = randint(1,6)
dice2 = randint(1,6)
if dice1 != dice2:
count += 1
else:
stop += 1
record.append(roll)
if count == 0 and stop == 1:
print("You roll same number for both dice at first try!")
else:
print(f"You roll same number for both dice after {count+1} attempts.")
print("The past record of dice rolling as below: ")
print(record)
【问题讨论】:
标签: python python-3.x list dictionary dice