How do I classify a float as an integer?

59 views Asked by At
a = int(input("Geef de a waarde van de vergelijking: "))
b = int(input("Geef de b waarde van de vergelijking: "))
if a == 0 and b != 0:
    print("Er is geen gehele oplossing.")
elif a == 0 and b == 0:
    print("Er zijn meerdere gehele oplossingen.")
elif a != 0 and b == 0:
    print("De gehele oplossing is x = 0.")
elif (b/a) != 0 and type(b/a) == int:
    print("De gehele oplossing is x = " + str(b/a) + ".")
elif (b/a) != 0 and type(b/a) != int:
    print("Er is geen gehele oplossing.")

I need to make y = ax + b and i need to tell which are the points where the function is zero. My problem lies with the last two elif's. When i put a = 6 and b = -54 it calculates it as the last elif: (b/a) != 0 and type(b/a) !=int. While when you calculate -54/6 you become -9. It's as if the programe doesn't count negative numbers as integers. I'm working with dodona and I've noticed in the correction that the only that are false are the ones that classify as elif (b/Ba) != 0 and type(b/a) == int. I know that when you devide its automatically a float type. But i don't know any other option to seperate the two. P.S. the print from the last elif says "there are no integer results". the print form the elif before that says "the integer result is x = (b/a) ."

I have tried to do it with the float type but i don't know how to classify it as an integer when it's an float.

2

There are 2 answers

0
Usman On

Use % and check for remainder. For floats remainder will be non-zero. You need to update like below. (No need for (b/a) != 0 as they are already checked for 0 in previous if/elif).

elif b % a == 0:
    print("De gehele oplossing is x = " + str(b/a) + ".")
elif b % a != 0:
    print("Er is geen gehele oplossing.")
0
seb On

As pointed out in the comments, python division operator / will always return a value of type float; for example 54/6 = 9.0.
To make the second to last elif do what you want, you can check if the remainder of the division is 0, since if that is the case, then the division yields an integer result.
The python modulo operator, %, does exactly this and thus you can use :

elif (b/a) != 0 and b % a == 0:

instead of

elif (b/a) != 0 and type(b/a) == int: