0

The code itself is using the variable x which starts as 0 and num which is x % 2.

The code takes x and adds 1 or 2 to x depending on user input. An example of the functionality is:

if num != int: print('hi') 

and a contrasting statement:

elif num == int: print("hello")

The code always prints "hi" no matter if num is a whole number or a fraction.

Red
  • 26,798
  • 7
  • 36
  • 58

2 Answers2

0

You want to use isinstance:

if not isinstance(num, int):

int is a type. You want to see if your number has that type, not if your number is the type int.

Kraigolas
  • 5,121
  • 3
  • 12
  • 37
0

What you're looking for is the built-in type() function:

num = 5

if type(num) != int: 
    print('hi') 
elif type(num) == int: # Ideally, this should just be "else:"
    print("hello")

Output:

hello

Do note that there exists another built-in function, isinstance(), that can do the same thing:

num = 5

if isinstance(num, int): 
    print('hello') 
else: 
    print('hi')

Output:

hello

BUT, you should keep in mind that this would also return True if num were the boolean value of True:

>>> isinstance(True, int)
True
Red
  • 26,798
  • 7
  • 36
  • 58