0

I have only very recently began to learn python or any programming language but I am struggling to understand why a piece of code behaves how it does.

I am following a course and in the course we are trying to create an app that determines if a number is odd or even.

This is what I wrote initially. I did this as I want to check that number % 2 was giving me the correct information - extremely new and just trying to understand the code I am using.

This failed

number = int(input("Which number do you want to check? "))

check = print(number % 2)

if check == 0:
    print("This is an even number")
else:
    print("This is an odd number")

However, when I removed print it worked as expected.

number = int(input("Which number do you want to check? "))

check = number % 2

if check == 0:
    print("This is an even number")
else:
    print("This is an odd number")

I was just curious as to why this might be?

Thank you!

  • 3
    Why would the result of `print` be meaningful here? Pay close attention to what you're assigning `check` *from*. Consider: `check = ...` and then `print(check)` on the next line. Key point: `x = y` and `x = f(y)` are two *entirely* different things. – tadman Jul 01 '23 at 23:58

1 Answers1

1

The result of the print function is always None. None == 0 will always be false.

Chris
  • 26,361
  • 5
  • 21
  • 42