如何计算Multiply倍python

时间:2017-08-15 13:51:00

标签: python collections counter

我是python的新手,并且有一些问题要问

问题是如何对抗倍增次数

有6个骰子支架

   import random
from collections import Counter

amt = 1
point = 0
diceStand = 6
dice1 = random.randint(1,6)
dice2 = random.randint(1,6)
dice3 = random.randint(1,6)
dice4 = random.randint(1,6)
dice5 = random.randint(1,6)
dice6 = random.randint(1,6)
roll = [dice1,dice2,dice3,dice4,dice5,dice6]

print("Do you want to play a dice game?")
print("turn " , amt , " of 3")
print("You have", point,'point')
print("You Roll")

for k in range(1):
    print(roll)
    counts = Counter(roll)
    print (Counter(counts))

结果:

[4, 1, 5, 6, 1, 4]
Counter({4: 2, 1: 2, 5: 1, 6*1})

我如何打印消息和计数器倍增时间 像4 * 3 = 12和1 * 2 = 2 只会超过2次

然后已经使用了4个骰子

但是你也可以滚动剩余的骰子,这是2次骰子一次 然后12和2将保存到这一点 3次尝试后将结束游戏

真的想了解如何使用随机值和计数器 谢谢

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您需要打印任何重复骰子卷的产品,例如如果你滚动一个' 2'四次,打印" 2 * 4 = 8"

如果是这样,那么这样的事情可以起作用:

import random

amt = 1
point = 0
diceStand = 6
dice1 = random.randint(1,6)
dice2 = random.randint(1,6)
dice3 = random.randint(1,6)
dice4 = random.randint(1,6)
dice5 = random.randint(1,6)
dice6 = random.randint(1,6)
roll = [dice1,dice2,dice3,dice4,dice5,dice6]

print("Do you want to play a dice game?")
print("turn " , amt , " of 3")
print("You have", point,'point')
print("You Roll")

for i in range(1,7):
    total = 0
    for die in roll:
        if i == die:
            total += 1
    print i, " * ", total, " = ", i*total

编辑:这是一个示例输出

Do you want to play a dice game?
('turn ', 1, ' of 3')
('You have', 0, 'point')
You Roll
1  *  1  =  1
2  *  0  =  0
3  *  2  =  6
4  *  2  =  8
5  *  1  =  5
6  *  0  =  0