所以基本上我需要创建一个标记为[0,1,2,3,4,5,10,25,100]的缓冲区列表。然后我需要在(0.75和4.75)+3之间创建随机数。现在我需要打印“缓冲3拍摄和平均 x 秒”之类的内容。 x 将是生成的随机数。每个缓冲区必须有自己的随机数
def main():
buffer= [0,1,2,3,4,5,10,25,100]
time= random.uniform(0.75,4.75)+3
sum=0
for i in range (1, 500):
sum = sum + time;
average= sum/1500
for buffer in range(0,1,2,3,4,5,10,25,100):
print("For Buffer",buffer,",the average time is",average,".")
[解决]
#Lab 1 using python.
Import random #Importing random number generator.
def main():
buffers= [0,1,2,3,4,5,10,25,100]; #Defining the buffers using list.
for buffer in buffers:
sum=0;
for i in range (1, 500): # Create a loop which will create random number and increment 500 times.
time= (random.uniform (0.75,4.75))+3
sum = sum + time
average= sum/500; #Finding the average of each buffers random numbers.
print("For Buffer",buffer,",the average time is",average,"."); #Print the output.
答案 0 :(得分:1)
您只需要更改它,以便在for循环中完成平均计算。
buffer= [0,1,2,3,4,5,10,25,100]
for buffer in range(0,1,2,3,4,5,10,25,100):
sum=0
for i in range (1, 500):
time= random.uniform(0.75,4.75)+3
sum = sum + time;
average= sum/1500
print("For Buffer",buffer,",the average time is",average,".")
答案 1 :(得分:0)
您可以使用字典将每个缓冲区与其平均值相关联,并使用缓冲区直接作为键,并将值与其平均值相关联。
def main():
buffer= [0,1,2,3,4,5,10,25,100]
time= random.uniform(0.75,4.75)+3
sum=0
dict_of_buffers = {}
for i in range (1, 500):
sum = sum + time;
average= sum/1500
for buffer in range(0,1,2,3,4,5,10,25,100):
print("For Buffer",buffer,",the average time is",average,".")
dict_of_buffers[buffer] = average