我试图制作一个非常简单的骰子游戏:
我需要它:
这是我到目前为止所拥有的:
def dice():
import random
list1 = []
for i in range (2):
x = random.randint(1,6)
list1.append(x)
if list1[0] == list1[1]:
print list1, "<-- DOUBLES"
return
else:
return list1
counter = 0
for i in range (10):
counter = counter + 1
print "Roll", counter, "was", dice()
print "Doubles were rolled", ### times: on roll x, y, and z
有什么建议吗?
答案 0 :(得分:1)
不是拥有一个连续的卷列表,而是将这对卷存储为列表中的两元组:
outcomes = []
for roll in range(rolls):
outcomes.append(tuple(random.randint(1, 6) for _ in range(2)))
或者,作为一个更加pythonic&#34;列表理解&#34;:
outcomes = [tuple(random.randint(1, 6) for _ in range(2))
for _ in range(rolls)]
然后您可以迭代以轻松找到对:
for index, roll in enumerate(outcomes, 1):
if roll[0] == roll[1]:
print("Roll {0} was a double {1}.".format(index, roll[0]))