【发布时间】:2023-01-27 02:03:13
【问题描述】:
我试图计算一系列 d6 卷值的平均值、平均值的平均值、一系列 d6 卷的总和以及一系列 d6 卷的平均总数。我的数字总是略有偏差,而且我所拥有的变量都让我感到困惑,而且我认为有些是多余的。如果有人可以帮助我清理我的代码和有用的东西。我扩展了 Stefan B 的代码,
```python
from random import randint
numRolls = int(input("How many times will you roll the dice? "))
s = 0
for roll in range(numRolls):
d1 = randint(1, 6)
d2 = randint(1, 6)
s += d1 + d2
print("You rolled a", d1, "and a", d2)
print("Your total for all of the rolls is", s)
print("Your average for all of the rolls is", s / numRolls)
```
并添加了一些我自己的代码,而不是掷两次骰子——我的代码:
```python
from random import randint
k = 0
x = 0
z = 0
v = 0
a = 0
j = 0
repeat = int(input("How many times would you like to repeat the following command? "))
numRolls = int(input("How many times will you roll the dice? "))
for roll in range(repeat):
s = 0
b = 0
for roll in range(numRolls):
d6 = randint(1, 6)
a += d6
s += d6
v = v + 1
y = s / numRolls
z += y
k += z
j = j + 1
print("Your total for all of the rolls is", s)
print("Your average for all of the rolls is ", s / numRolls)
rollsets = numRolls / 6
print("Your total average for the total of all rolls is ", z / v)
print("Your total average for all of the rolls is ", a / v)
#print(y)
#print(x)
#print(v)
#print(z)
#print(k)
```
我想做它在那里做的事情,乘以 repeat 和 numRolls 变量,并计算出一小组卷的总数和平均值,以及所有卷的平均值和总数的平均值。
我试图使用字母变量来获得更好的平均值,但失败了。我想我有多余的东西,而且我无法弄清楚如何衡量总数的卷数和“卷集”。请帮忙?
【问题讨论】:
-
请标记一种语言。如果这是 python,请注意正确的缩进是必不可少的。阅读帮助以了解如何正确格式化代码。
标签: python random average dice