Search code examples
python-3.xfloating-pointintfizzbuzz

Python seems to make every variable a float


I tried to write a simple fizzbuzz program in python, I tried to use isinstance to determine if a number is a float or a int instead of creating a list, but whenever I try it for some reason it considers every number to be a float is this a thing in python or a flaw in my logic?

i = 3
if isinstance(i/3,float) == False:
    print("Fizz")
else:
    print("???")

Solution

  • The / operator returns a float. Even if the value could be expressed by an integer, you'll still get a float. E.g., 3/3 will return the float value of 1.0. Instead, you could check the remainder directly using the % operator:

    if i % 3 == 0:
        print("Fizz")
    else:
        print("???")