2

I'm (very) new to all of this programming stuff and I need help with a program I'm making in Python 3. It's designed, at the moment, to find how many numbers between 1 and 10 are divisible by 5. This is my approach:

def five():
    a = 0
    b = 0
    c = 0
    while a <= 9:
        a = a + 1
        b = a / 5
        if type(b) == int and b is not 0:
            c = c + 1
        else:
            pass
    print c

In this case it's printing "6".

The problem is, well, as you might already know, in Python the number 1.0 is not an integer. The only thing I want is to make Python know all numbers with a 0 after the dot are integers, or find an interactive programming language who does, or find another approach.

Thanks!

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
User682
  • 23
  • 3
  • 4
    Are you *sure* you're using Python 3? In Python 3, `print c` is a syntax error. Python 3 requires `print(3)` because `print` is a function. – Greg Hewgill Nov 05 '11 at 02:29
  • Teacher: "How many of these numbers are divisible by 2? Fred? I know you know." Fred: "Uhm, all of them." (from Little Man Tate) – PaulMcG Nov 05 '11 at 05:40

6 Answers6

6

Asking for the type() of a value in Python is not going to tell you whether it is a round integer or not. The resulting type of a calculation doesn't change type depending on the answer. (But in Python 2, the type of the answer in division depends on the type of the inputs. In Python 3, the type after / is always float, while the type after // depends on the type of the inputs.)

To test for an integer divisible by 5, use the modulo operator:

if a % 5 == 0:
    c = c + 1

Also, avoid using the is operator with integers. Use b != 0 to compare with zero, instead of b is not 0 (see Python “is” operator behaves unexpectedly with integers for the gory details).

Community
  • 1
  • 1
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • The looping is also vastly overcomplicated. All that is needed is something like `[x % 5 for x in range(1, 10)].count(0)`. – Karl Knechtel Nov 05 '11 at 07:48
2

I suggest you use

if x % 5 == 0
    ...

to find those exactly divisible by 5. % is modulo operator, it gives the remainder to the integer division, this remainder is only 0 where the division is exact.

ewanm89
  • 919
  • 5
  • 22
1

You need to use the mod '%' operator. Example

10 % 5 = 0

6 % 5 = 1

Since you're newbie, here's a short snippet as you may need a helping hand.

count = 0
for i in range(1,10):
    if i % 5 == 0:
        count += 1
print count
Alvin K.
  • 4,329
  • 20
  • 25
1

A slightly different way to do this is:

def five():
    count = 0
    for i in range(1, 10):
        if i%5 == 0:
            count += 1
    print (count, end='')
  1. Use generator to generate the numbers
  2. Use the modulus operator to check the remainder after division
  3. Python 3 uses brackets for printing.
Abizern
  • 146,289
  • 39
  • 203
  • 257
1

There is a method to tell whether a float is integral (i.e. x == int(x)):

>>> (1.0).is_integer()
True
>>> (1.1).is_integer()
False

So, you can replace type(b) == int with b.is_integer() and your script will work fine.

Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
0

How about a one liner:

sum([0 if x%5 else 1 for x in range(1,10)])
Mike
  • 2,429
  • 1
  • 27
  • 30
  • No []'s required, unless you are on old version of Python. And even simpler is to just use 'not': `sum(not x%5 for x in range(1,10))` – PaulMcG Nov 05 '11 at 06:44