lundi 18 octobre 2021

Trying to find the average of a randomized list in python [duplicate]

I'm a computer science student in high school and one of my assignments is to find how many times a number appears in a randomized list and the average of said list using functions. I got the first part done but I can't figure out the average. Any tips?

import random
list = []   
for i in range(10000):
    list_num = random.randint(1,6)
    list.append(list_num)
def count_list(list, number):
    num = 0
    for i in range(len(list)):
        if list[i] == number:
            num += 1
    return num
def sum(list):
  total = 0
  for a in list:
    total = total + a
  return total
def avg(sum):
    avg = total // 10000
print("There are:", count_list(list, 1), "ones.")
print("There are:", count_list(list, 2), "twos.")
print("There are:", count_list(list, 3), "threes.")
print("There are:", count_list(list, 4), "fours.")
print("There are:", count_list(list, 5), "fives.")
print("There are:", count_list(list, 6), "sixes.")
print()
print ("The average is", avg(sum))



Aucun commentaire:

Enregistrer un commentaire