HappyHands31 HappyHands31 - 1 month ago 18
Python Question

If Statement - Nested Conditional Using Modulus Operator in Python

I need to create a program in Python that asks the user for a number, then tells the user if that number is even or if it's divisible by 5. If neither is true, do not print anything. For example:

Please enter a number: 5

This number is divisible by 5!

Please enter a number: 7

Please enter a number: 20

This number is even!

This number is divisible by 5!


I tried to copy the method that was used in this answer, but I'm getting an error message on line 8:

SyntaxError: invalid syntax (<string>, line 8) (if Num_1 % 2 == 0)


Here is my code:

#TODO 1: Ask for user input
Num1 = input("Please enter a number")
#TODO 2: Turn input into integer
Num_1 = int(Num1)
#TODO 2: Use conditionals to tell the user whether or not their
#number is even and/or divisible by 5

if Num_1 % 2 == 0
print ("This number is even!")
if Num_1 % 5 == 0
print ("This number is divisible by 5!")


Since I'm using the modulus operator to determine whether or not Num_1 is an exact multiple of 2, I should be returning a value of True and therefore should print "This number is even!" But instead I'm getting this error message - why? Thanks!

Answer

The start of each Python block should end with a colon :. Also take note of the indentation.

Num1 = input("Please enter a number")
Num_1 = int(Num1)

if Num_1 % 2 == 0:
    print ("This number is even!")
    if Num_1 % 5 == 0:
        print ("This number is divisible by 5!")