0

My code:

for i in range( 3.3, 5 ):
        print( i )

The above code have to print:

3.300000

4.300000

but the interpreter of Python 3.4.0 printed the following error:

TypeError: 'float' object cannot be interpreted as an integer

1
  • 1
    range() works with integers, not fractional values. Commented Oct 26, 2015 at 21:07

1 Answer 1

1

range() works with integers not floats, but you can build your own range generator which will do what you want:

def frange(start, stop, step=1):
    i = start
    while i < stop:
        yield i
        i += step

for i in frange(3.3, 5) will give you the desired result.

Note though, that frange will, unlike range but like xrange, return a generator rather than a list.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.