我对Python很陌生并试图计算一堆随机数的平均值遇到了障碍。该程序的一般概述是它是一个模具滚动程序,它提示用户输入某个然后滚动,直到程序输出蛇眼。它还记录了双打的数量和获取蛇眼所需的时间。然后创建整个程序中为每个模具滚动的数字的平均值。这就是我迷失的地方。
编辑:我删除了原始代码并在Vasilis的答案中工作: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)
然而,正如他所指出的那样,它不会记录通过所有卷记录的所有数字,而是记录最后一个数字。 任何帮助实现这一点将不胜感激。
谢谢!
答案 0 :(得分:0)
如果你想看一下,我在代码中做了一些更改。我假设你想要将平均值计算为整数,尽管你可以改变它。您使用die_1
和die_2
作为简单的整数变量,因此它们只保留random.randint(1,user_s)
生成的最后一个值:
import random
'''Skipped input part'''
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("DIE_1: ", die_1)
print("DIE_2: ", die_2)
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", avg_roll1)
print("The average roll for die 2 was", avg_roll2)
答案 1 :(得分:0)
自己动手。
将累加器变量=设置为模具辊。在这种情况下,sum1和sum2是变量。
# make all of the functions in the "random" module available to this program
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 statement for die rolls
while True:
roll_count += 1
die_1 = random.randint(1,user_s)
sum1 += die_1
die_2 = random.randint (1,user_s)
sum2 += die_2
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
print("You finally got snake eyes on try number", roll_count)
print("Along the way you rolled a double", double_count,"times")
# 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", format(avg_roll2,".2f"))