3,518 views, 0 RAMs, and 2 comments
I have an issue with my code in Python. I keep getting an error about the variable type: "TypeError: 'float' object cannot be interpreted as an integer". I do understand what the code says but I have no idea how to resolve it. Any help is appreciated
This is a debug account.
One possible reason why you're running into this error is because of a float being divided to a cleanly-rounded number, but that result still being a float. This happens with the
range() function, for example.
>>> x = 4.0 >>> y = x / 2 >>> print x 4.0 >>> print y 2.0 >>> for i in range (y): ... print "y loop" ... Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: range() integer end argument expected, got float.
In this scenario, you can cast the float to an integer. Note that there are generally better options than typecasting values that will prevent errors from showing up further down the line:
>>> for i in range ( int(y) ): ... print "y loop" ... y loop y loop
Sir, I can do you a nice SEO.
When we try to use float numbers in Python
range() function, we get a type error 'float' object that cannot be interpreted as an integer.
One option is is to convert float to int and use it in range. But that is not the ideal way. So you should use write custom range() function that will allow float arguments. you can use NumPy’s
linspace() functions to generate the range of float numbers. Refer to this range for float numbers
import numpy x = 2.0 y = 10.5 for i in numpy.arange(x, y): print(i, end=', ')
Post a New Comment
Do you like having a good time?
Register an Account
Read Quality Articles
Read some quality articles. If you can manage to not get banned for like five minutes, you can even post your own articles.
Argue with People on the Internet
Use your account to explain why people are wrong on the Internet forum.
Vandalize the Wiki
Or don't. I'm not your dad.
Ask and/or Answer Questions
If someone asks a terrible question, post a LMGTFY link.
Make Some Money
Hire freelancers and/or advertise your goods and/or services. Hire people directly. We're not a middleman or your dad. Manage your own business transactions.
Answers— Read More
Find more related content below!
- [Python] Solution to "ValueError: unknown label type: 'continuous' or 'unknown'?"...
- [Python] Help with "ValueError: length of values does not match length of index?"
- [Python] Help with "ValueError: cannot reindex from a duplicate axis?"
- [Python] Solution to "ValueError: cannot convert float nan to integer?"
- [Python] Solution to "TypeError: a bytes-like object is required, not 'str'?"
- [Python] How to fix "TypeError: 'str' object does not support item assignment?"
- [Python] Help with "TypeError: 'float' object cannot be interpreted as an integer?"
- [Python] Fix for "NameError: name 'xrange' is not defined?"