Finding float average of random list in python

Viewed 195

I have looked on several websites, books, and in the documentation and I can't figure out what I am doing wrong. I try to ask for help as a last resort, so that I can learn on my own, but I have spent far too long trying to figure this out, and I am sure it is something really simple that I am doing wrong, but I am learning. The code produces a single different result every time it is ran. The code produces the following error: 26.8 Traceback (most recent call last): File "main.py", line 7, in tot = sum(rand)/len(rand) TypeError: 'float' object is not iterable

import random
for x in range (10000):
   rand = random.uniform(10, 100)
   print(round(rand, 1))

   tot = sum(rand)/len(rand)
   print (round(tot, 1))
2 Answers

You're not actually generating a list, you're generating individual values. Do you really want to print out 10000 values along the way to your final result?

If the answer is "no!", then your code can be reduced to:

import random

N = 10000
print(round(sum(random.uniform(10, 100) for _ in range(N)) / N, 1))

or, if you prefer to break it out a little bit more for readability:

import random

N = 10000
total = sum(random.uniform(10, 100) for _ in range(N))
average = total / N
print(round(average, 1))

If this is beyond the scope of what you've learned, you can create total outside the loop initialized to zero, update it with each new value as you iterate through the loop, and then calculate the final answer:

import random

N = 10000
total = 0.0
for _ in range(N):     # use '_' instead of x, since x was unused in your prog
    total += random.uniform(10, 100)
average = total / N
print(round(average, 1))

This avoids wasting storage for a list of 10000 values and avoids the append() you're not yet familiar with. Of course, if you need the 10000 values later for some other purpose, you'll need to tuck them away in a list:

import random

N = 10000
l = [random.uniform(10, 100) for _ in range(N)]
total = sum(l)
print(round(total / N, 1))

Addendum

Just for jollies, you can also do this recursively:

import random

def sum_of_rands(n):
    if n > 1:
        half_n = n // 2
        return sum_of_rands(half_n) + sum_of_rands(n - half_n)
    elif n == 1:
        return random.uniform(10, 100)

N = 10000
print(round(sum_of_rands(N) / N, 1))
print(sum_of_rands(0))  # returns None because nothing is being summed

Splitting the problem in half (on average) in each recursive call keeps the stack to O(log N).

I'd actually advise you to stick with list comprehension or looping, but wanted to show you there are lots of different ways to get to the same result.

In the sum function you must parse an iterable object but you're parsing a float object. To avoid this error you should put two last lines outside the for loop and append rand to a list. I don't know if it's what you want to do but it shows you how use sum:

import random
l = []
for x in range(10000):
    rand = random.uniform(10, 100)    
    l.append(rand)
    print(round(rand, 1))

tot = sum(l)/len(l)
print(round(tot, 1))
Related