我真的很困惑我在这里做的事我不知道为什么它不适合我。 我可以打印数字1-6的随机卷,但现在我正在尝试做另外两件事:
import random
def rollDie(number):
rolls = [0] * 6
for i in range(0, number):
roll=int(random.randint(1,6))
rolls[roll - 1] += 1
return rolls
if __name__ == "__main__":
result = rollDie(50)
i = result
print (result)
print(i/6)
答案 0 :(得分:1)
你提出的第一个问题需要一些澄清;你正在寻找的是什么?#34;平均值"价值抛出?要回答第二个问题,您给我们的功能会进行数量数量的骰子滚动,将它们保存到与每个骰子编号对应的列表中。所有你需要做的就是打印最流行的面部是打印列表并寻找最大的数字。或者,您可以使用array.index( max( array ) )
查找索引或“#face”'最卷的脸。
import random
def rollDie(number):
rolls = [0] * 6
for i in range(0, number):
roll=int(random.randint(1,6)) # try to avoid naming things like this
rolls[roll - 1] += 1
return rolls
if __name__ == "__main__":
result = rollDie(50)
i = result
print (result) # you can just look at the largest value in the array
print(i/6) # this is dividing a list by an integer....
print result.index(max(result)) # This will print the index of the post rolled face
# i.e if one was the most rolled it would print 0
更明确地说明你应该拥有的是:
import random
def rollDie(number):
rolls = [0] * 6
for i in range(0, number):
roll=int(random.randint(1,6))
rolls[roll - 1] += 1
return rolls
result = rollDie(50)
print (result)
print result.index(max(result))
答案 1 :(得分:0)
试试这个:
import random
def rollDie(number): # cleaner version
results = []
for i in range(0, number):
results.append(random.randint(1, 6))
return results
def most_common(lst): # quick dirty lambda to calc the most occurring roll
return max(((item, lst.count(item)) for item in set(lst)), key=lambda a: a[1])[0]
results = rollDie(50) # 50 rolls
total = 0 # creating total
for i in results:
total += i # adding up all the dice rolls
print(total) # total
print(total / 50) # Average from the 50 dice rolled
print(round(total / 50)) # rounded Average
print(most_common(results)) # most occuring number