0

I am getting an error as 'float' object cannot be interpreted as an integer

num=5
for x in range(num/2):
     print("hello")
jmd_dk
  • 12,125
  • 9
  • 63
  • 94
  • 2
    what are you trying to do? You can't execute the print two and a half times – bb1950328 Jun 02 '20 at 12:19
  • @Sadap: I was writing a program to find whether a number is prime or not. As a part of that, i want to divide that number from 1 to half of that number so that I can know the factors of that number. If the number of factors is not greater than 2 I can decide it as a prime number. So in place of range, I want to take num/2. for example like in C language: x=10 for(i=1,i – Suneeldatta Kolipakula Jun 02 '20 at 12:42
  • int/int in C is the same as int//int in Python3. – bb1950328 Jun 02 '20 at 12:44
  • 1
    Instead of half of the number, you should loop till the square root of the number. Not related to the question but I thought I should point out. Check this out for more information - https://cp-algorithms.com/algebra/sieve-of-eratosthenes.html#toc-tgt-3 – paradocslover Jun 02 '20 at 12:45
  • @paradoxlover: u mean to say that I need to iterate my for loop till the square root of a given number to find it prime or not?? example if my number is 16 then is it enough to run for loop till 4?? – Suneeldatta Kolipakula Jun 02 '20 at 12:49
  • 1
    @SuneeldattaKolipakula Yup, check this out for the reason - https://stackoverflow.com/a/5811176/8293309 – paradocslover Jun 02 '20 at 12:52
  • @paradoxlover: thank you so much really interesting. Not related to the question can u please tell me why can't I use as : for x in range (int(num/2)) to convert the resulting floating-point number into integer?? for suppose if I want to use math.sqrt() function inside the range how to use it?? Basically my question is how to typecast the expression that we mention in range?? – Suneeldatta Kolipakula Jun 02 '20 at 13:00
  • 1
    @SuneeldattaKolipakula You could have used `int(num/2)` which is similar to `num//2`, but num//2 is better (readability and time). Now, coming to your question, instead of using `sqrt` and `range`. Use `while` with the condition `i*i<=n`. – paradocslover Jun 02 '20 at 13:10

1 Answers1

6

You should use integer division

num=5
for x in range(num//2):
     print("hello")

range does not support float input. If you want to use floating value, consider using numpy's arange.

paradocslover
  • 2,932
  • 3
  • 18
  • 44
  • Note that the behavior of `int/int` is different in Python 2, where it *does* result in another integer. The code above works in both Python 2 and 3 though. – jmd_dk Jun 02 '20 at 12:22
  • I understand the difference. But since the OP has an error `'float' object cannot be interpreted as an integer`, it was obvious that Python3 is being used. Had it been python2, `num` and 2 both being `integer` would have resulted in an `integer` value - and there would have been no error – paradocslover Jun 02 '20 at 12:25
  • This avoids the exception but from the question it is entirely unclear if they want to iterate over `range(2)`, `range(3)` or something else entirely – Chris_Rands Jun 02 '20 at 12:27
  • 1
    You are right but it does answer the question - `why cant we pass arithmetic expression in range in for loop in python. Getting error` . we can pass arithmetic expression! – paradocslover Jun 02 '20 at 12:29