0

It doesn't matter what number I enter, d variable is somehow always 326, please help I'm a beginner.

a=int(input("enter a number:"))
c=0
d=0
while a>0:

    b=a%10
    a=a/10
    
    if b%2==0:
        c=c+1
    
    else:
        d=d+1

print("even numbers:",c)
print("odd numbers:",d)

This is pretty much it

1 Answers1

1

You should use int division: a=a//10. Otherwise it will loop until Python can't tell the difference between a=0.00000.... and 0. which is approximately 326 digits.

Note that if you input 10000 you get 324 as output

Edit: I found in another SO answer the smallest positive float you can get:

np.nextafter(0, 1)
4.9406564584124654e-324

Which confirms my first interpretation.

In the comments, @Joshua also pointed out the differences between Python 2 and 3 regarding division.

Tranbi
  • 11,407
  • 6
  • 16
  • 33
  • 1
    Note that this is a critical difference between Python3 and Python2 -- Python2 treats the `\\` operator as integer division, where Python3 treats it as real division, and coerces (yay dynamic typing!) the operators to real numbers (from integers) as needed. – Joshua Voskamp Oct 20 '21 at 17:25
  • @JoshuaVoskamp you mean `/` – mkrieger1 Oct 20 '21 at 17:30