[Tutor] TypeError: 'float' object is not iterable

Alan Gauld alan.gauld at yahoo.co.uk
Thu Jul 23 03:11:01 EDT 2020


On 23/07/2020 00:33, hongerlapjes wrote:
> I'm learning python and need help. How can I sum up "bedrag"?
> I've replaced the "." with a "," and changed the string into a float, 
> but I can't iter. I'm lost. What do I need to do?

Your question does not make much sense.
You have made bedrag a float. That is a single number.
How do you sum a single number?

Lets look at what you are doing...

>          #change string into float and replace the comma with a dot
>          bedrag = float(row['Bedrag (EUR)'].replace(",", "."))

Is like saying

bedrag = 1.234

>              #try 1 gives: TypeError: 'float' object is not iterable
>              som = sum(bedrag)

som = sum(1.234)

Does that make sense?

>              #try 2 gives: TypeError: iteration over a 0-d array
>              arr = np.array(float(row['Bedrag (EUR)'].replace(",", ".")))

arr = np.array(1.234)

How would that work? What kind of array do you expect?

>              #try 3 gives: TypeError: 'float' object is not iterable
>              arr = np.array(list(float(row['Bedrag (EUR)'].replace(",", 
> "."))))

arr = np.array(list(1.234))

What kind of list does list(1.234) create?

sum() needs a sequence but you have a single value.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list