2

I have code:

  a = int(input("Type a integer:"))
    temp = []
    while a != 1:
  --->   for i in range(2, a):
            if a % i == 0:
                temp.append(i)
                a = a / i
                break
    print(temp)

I typed 60 and then it gives the error: TypeError: 'float' object cannot be interpreted as an integer.

However, I checked:

a = int(input("Type a integer"))
type(a)

It shows type of a is int.

If so, where is the float type comes from?

2
  • 2
    You're dividing! a will become a float if it's not divisible by i (creating fractional parts). Commented Aug 10, 2017 at 18:48
  • 1
    a = a // i if you want integer division Commented Aug 10, 2017 at 18:50

3 Answers 3

4

When you divide a by i, it becomes a decimal number after so many iterations. If you started with 60, then the operations would follow like this:

a = 60 / 2     (=30)
a = 30 / 3     (=10)
a = 10 / 4     (=2.5)

Now, we can see that a is trying to be a float when you told it to be an integer.

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

Comments

1

When you divide it by a number , it become a float , you can return it to integer by :

a = int(a)

after

a = a/i

Or :

 a = a//i

Comments

1

The float comes from using float division a/i. To get an int use integer division a//i

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.