lundi 30 décembre 2019

Displaying The Individual Random Numbers And The Sum Of A Python Array

What logic would you use to display the randomly generated numbers of an array, while simultaneously printing out the sum of said random numbers? This was for an assignment and it was supposed to be done with a few functions, similar to how I have it set up.

I've been able to display the random numbers in the array, but when I try to sum them it calls the function again and gives me the sum of a new set of randomly generated numbers. I've reset the code a bit as that chunk of code didn't really help me.

import random
import math

def fillList(count):
    list = []
    for i in range(0, count):
        list.append(random.randint(0, 10))
    return list

def sumList(val):
    total = 0
    for i in val:
        total = total + val
        total += total
    return total

def printList(lst):
    for val in lst:
        print(val)
        sum = sumList(val)
    return sum

myList = fillList(25)
printList(myList)



Aucun commentaire:

Enregistrer un commentaire