256 Kilobytes

Answers in Scripting and Automation | By Some Guy

Published 1 year agoSun, 13 Jan 2019 05:40:20 -0800

2,274 views, 0 RAMs, and 2 comments

Tags: Python, Scripting, Errors, Programming, TypeError

Profile Photo - Some Guy Some Guy
🗎 3224 🗨 43 🐏 31

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

Download more RAM. 🐏 ⨉ 0 Posted by Some Guy 1 year ago 🕓 Posted at 13 January, 2019 05:40 AM PST

This is a patsy account used by the 256 Kilobytes staff to seed content. 

Profile Photo - August R. Garcia August R. Garcia LARPing as a Sysadmi... Portland, OR
🗎 213 🗨 1116 🐏 337
Site Owner

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
>>> print y
>>> 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
Users Who Have Downloaded More RAM:
Huevos Rancheros (1 year ago)
🐏 ⨉ 1
Posted by August R. Garcia 1 year ago 🕓 Posted at 31 January, 2019 20:42 PM PST

Sir, I can do you a nice SEO.

Profile Photo - rahuldev rahuldev
🗎 0 🗨 1 🐏 0

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 arange() and 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=', ')
Download more RAM. 🐏 ⨉ 0 Posted by rahuldev 1 week ago 🕓 Posted at 24 March, 2020 04:03 AM PDT

Post a New Comment

To leave a comment, login to your account or create an account.

Do you like having a good time?

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.

View Articles →

Argue with People on the Internet

Use your account to explain why people are wrong on the Internet forum.

View Forum →

Vandalize the Wiki

Or don't. I'm not your dad.

View Wiki →

Ask and/or Answer Questions

If someone asks a terrible question, post a LMGTFY link.

View Answers →

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.

Register an Account
You can also login to an existing account or recover your password. All use of this site is subject to terms outlined in the terms of service and privacy policy.