我对Python相当陌生,在尝试计算一堆随机数的平均值时遇到了一个障碍数字。那个该程序的总体概述是,它是一个模具轧制程序,它提示用户输入一定数量的边,然后滚动,直到程序输出蛇眼。它还可以记录下双打的数量以及蛇眼花了多长时间。然后创建在整个程序中为每个骰子掷骰子的平均数。这就是我迷路的地方。在
编辑:我删除了我原来的代码,用瓦西里斯的回答:import random
while True:
#prompts user for valid number
user_s = int (input ("How many sides on your dice? "))
if user_s < 3:
print("That is not a valid size value, please enter a positive number")
if user_s >= 3:
break
print()
print("Thanks! Here we go...")
print()
double_count = 0
roll_count = 0
sum1 = 0 # Used to calculate sum of die_1
sum2 = 0 # Used to calculate sum of die_2
while True:
roll_count += 1
die_1 = random.randint(1,user_s)
die_2 = random.randint (1,user_s)
print(roll_count,".", " Die number 1 is ", die_1 , " and die number 2 is ", die_2, ".", sep ="")
if die_1 == die_2:
double_count += 1
if die_1 == 1 and die_2 == 1:
break
# Making sum
sum1 = sum1 + die_1
sum2 = sum2 + die_2
print("You finally got snake eyes on try number", roll_count)
print("Along the way you rolled a double", double_count,"times")
print(die_1)
print(die_2)
# Integer divisions
avg_roll1 = sum1 // roll_count
avg_roll2 = sum2 // roll_count
print("The average roll for die 1 was", format(avg_roll1,".2f"))
print("The average roll for die 2 was", avg_roll2)
然而,正如他所指出的,它并不是把所有的数字都记录在所有的卷上,而是最后一个数字。
如果有任何帮助,我们将不胜感激。在
谢谢!在
如果觉得《python产生10个不同的随机数组成列表并求平均值_计算多个随机数的平均数Python...》对你有帮助,请点赞、收藏,并留下你的观点哦!