308-8 AND 308-9 Learn to Measure Variability


sample_devs = []


for i in range(5000):
    sample = houses['SalePrice'].sample(10, random_state=i)
    st_dev = standard_deviation(sample)
    sample_devs.append(st_dev)
    
    
plt.hist(sample_devs)
plt.axvline(standard_deviation(houses['SalePrice']))

So I am getting a weird error that I don’t think is an issue with my code as I just checked the answer to make sure I was not crazy. I think this is a bug. The error I get is:

TypeErrorTraceback (most recent call last)
<ipython-input-1-b7fdbfeb43e5> in <module>()
     16 
     17 
---> 18 for i in range(5000):
     19     sample = houses['SalePrice'].sample(10, random_state=i)
     20     st_dev = standard_deviation(sample)

<ipython-input-1-d7b275ef9597> in range(array)
      3 
      4 def range(array):
----> 5     variability = max(array) - min(array)
      6     return variability
      7 

TypeError: 'int' object is not iterable

Can someone confirm that this is a bug or point out the error I am overlooking?

Many Thanks!

Edit I am also seeing this same error appear on 308-9 Bassel’s Correction. Same error and everything.

Hey, Clara. This answer is divided into two parts: clarification of the problem, and solution.

Determining the problem

Judging by the part of the second part of the error, it looks like you overwrote the definition of the built-in function range:

def range(array):
    variability = max(array) - min(array)
    return variability

So when you try to run for i in range(5000), it passes 5000 to your range function, not to the built-in one. When it tries to compute max(5000), it yields an error because max expects an iterable and integers aren’t iterables.

It’s possibly you created this function in a previous screen in this mission and it propagated to the following screens.

Solution

So how do you fix this? Three ways come to mind:

  1. You don’t do anything, you just open the link and try again. I think this will work because the code runner will have been reset and your definition of range won’t be in memory anymore (I hope).

  2. You call the built-in function directly:

    for i in __builtins__.range(5000):
        sample = houses['SalePrice'].sample(100, random_state = i)
        st_dev = standard_deviation(sample)
        st_devs.append(st_dev)
    
  3. You assign the built-in range function to range. Two ways to do this follow below.

    • Running del range will delete your function’s definition and make range default to the built-in function.
    • Running range = __builtins__.range will overwrite your function’s definition with that of the built-in function.

I hope this helps.

3 Likes

What a lapse in good judgement overwriting the built-in function. In my mind, it was just for the screens that involved us using our own functions. Lesson learned! It appears the code runner refreshed and my oops is corrected. Thanks!

1 Like