# 'float' object is not iterable

My Code:

``````def freq_table (dataset, index):
frequency ={}
total  = 0
for row in dataset :
total +=1
value = row [index]
if value in frequency:
frequency [value] += 1
else :
frequency [value] = 1

frequency_percents = {}

for key in frequency :
percent_frequency = (frequency [key]) / total * 100
frequency [key] = percent_frequency
return percent_frequency

def display_table(dataset, index):
table = freq_table(dataset, index)
table_display = []
for key in table:
key_val_as_tuple = (table[key], key)
table_display.append(key_val_as_tuple)

table_sorted = sorted(table_display, reverse = True)
for entry in table_sorted:
print(entry, ':', entry)

``````

What I expected to happen:
Display a frequency table for specific categories

What actually happened:

``````'float' object is not iterable
``````

I copied and pasted the code from the problem

Hello,

In the first function freq_table, you’re returning the value inside the second for loop “percent_frequency” whereas you’re supposed to return the dictionary “frequency”. You should change this line in that function:

``````return percent_frequency
``````

into this:

``````return frequency
``````

Because in the next function, you should use this function to create a table of the dataset in the first line.

It should be `return frequency_percents`

I wrote the correct code below with the comment to explain what each step is doing.

``````frequency_percents = {} #you create a dictionary here, good

#you loop over the frequency dict and populate the frequency_percents dict
for key in frequency :
#the percent_frequency is only a temporary variable that you use
#to populate the frequency_percents dict
percent_frequency = (frequency [key]) / total * 100
frequency [key] = percent_frequency
# you need to return the frequency_percents dict that you have populated
return frequency_percents

``````

The problem is that you created the `frequency_percents` dictionary but you never used it. In the code below, you are appending the `percent_frequency` variable to the same dictionary you used in the first loop, not the one you just created.

`````` frequency_percents = {}

for key in frequency :
percent_frequency = (frequency [key]) / total * 100
frequency [key] = percent_frequency
return percent_frequency
``````

Also, your function returns the `percent_frequency` variable, which is a float. When the `display_table` function receives the object and tries to iterate over it, it raises the error you see, stating that the object is a float, therefore not iterable.

To fix this, you need to append the `percent_frequency` to the `frequency_percents` dictionary and then return the dictionary. The `freq_table` function would then look like this:

``````def freq_table (dataset, index):
frequency ={}
total  = 0
for row in dataset :
total +=1
value = row [index]
if value in frequency:
frequency [value] += 1
else :
frequency [value] = 1

frequency_percents = {}

for key in frequency :
percent_frequency = (frequency [key]) / total * 100
frequency_percents [key] = percent_frequency
return frequency_percents
``````

Also, I’d suggest you choosing variable names that are not too similar to each other as this might confuse you while you are writing your code, making you use wrong variables.

1 Like