[英]For every 10th in a array.count(1)
So I got a array of die rolls 1 to 6.所以我得到了一系列 1 到 6 的骰子。
So I count the times 1 runs in the array with .count, but I also have to print something for every time that count passes a 10. So if I roll 100 times and I get 23 rolls of 1, I want it to show a * for each 10 in it.所以我用 .count 计算 1 在数组中运行的次数,但我也必须在每次计数超过 10 时打印一些东西。所以如果我滚动 100 次并且得到 23 卷 1,我希望它显示一个* 每 10 个。
The code so far is:到目前为止的代码是:
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)
and I get:我得到:
What is the maximum die rolls: 100
100 total rolls.
1: [21 | 20.8%]
329
I need it to look like:我需要它看起来像:
What is the maximum die rolls: 100
100 total rolls.
1: ** [21 | 20.8%]
329
Do you mean like this?你的意思是这样吗?
stars = "*" * (die_rolls.count(1) // 10)
print("1: " + stars + " [" + str(die_rolls.count(1)) + " |", "{:.1f}".format(percent_one) + "%]")
The // operator performs integer division, so 19//10 = 1, 20//10 = 2, 21//10 = 2, etc. // 运算符执行整数除法,因此 19//10 = 1、20//10 = 2、21//10 = 2 等。
Writing "*" * 2 makes Python repeat the string "*" twice.写 "*" * 2 会使 Python 重复字符串 "*" 两次。
stars = '*' * (die_rolls.count(1) // 10) # count number of stars
print('1: {0} [{1} | {2:.1f}%]'.format(stars, die_rolls.count(1), percent_one))
EDIT: as suggested by @Ev.Kounis save die_rolls.count(1) to a variable.编辑:正如@Ev.Kounis 所建议的那样,将 die_rolls.count(1) 保存到一个变量中。 Eg
count_one = die_rolls.count(1)
例如
count_one = die_rolls.count(1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.