对于array.count中的每10个(1)

时间:2017-10-18 14:57:41

标签: python arrays count

所以我得到了一系列1到6的模具卷。

因此,我计算在数组中使用.count运行的次数为1次,但是每次计数通过10时我也必须打印一些东西。所以如果我滚动100次并得到23卷1,我想要它在其中显示每个10的*。

目前的代码是:

import random

die_rolls = []

maxsize = int(input("What is the maximum die rolls: ")) + 1

for num in range(1,maxsize):
    die_random = random.randint (1,6)
    die_rolls.append(die_random)

print(str(maxsize-1) + " total rolls.")
percent_one = die_rolls.count(1) / maxsize *100

print("1: " + "[" + str(die_rolls.count(1)) + " |", "{:.1f}".format(percent_one) + "%]")

total=sum(die_rolls)
print(total)

我得到了:

What is the maximum die rolls: 100
100 total rolls.
1: [21 | 20.8%]
329

我需要它看起来像:

What is the maximum die rolls: 100
100 total rolls.
1: ** [21 | 20.8%]
329

2 个答案:

答案 0 :(得分:0)

你的意思是这样吗?

stars = "*" * (die_rolls.count(1) // 10)
print("1: " + stars + " [" + str(die_rolls.count(1)) + " |", "{:.1f}".format(percent_one) + "%]")

//运算符执行整数除法,因此19 // 10 = 1,20 // 10 = 2,21 // 10 = 2等

写“*”* 2使Python重复两次字符串“*”。

答案 1 :(得分:0)

stars = '*' * (die_rolls.count(1) // 10)  # count number of stars
print('1: {0} [{1} | {2:.1f}%]'.format(stars, die_rolls.count(1), percent_one))

编辑:正如@ Ev.Kounis所建议的那样将die_rolls.count(1)保存到变量中。 例如。 count_one = die_rolls.count(1)