-1

I am new to Python. I have learned the If else statement, sto this exercise has to be with the If else and not with something more.

Write a program which asks the user to type an integer and then prints "Yes" if that integer is divisible by 3, otherwise prints "No"

My code is this:

user_input = input("Type an integer: ")
user_int = int(user_input)
if user_int / 3:
    print("Yes)
else:
    print("No")

I think I messed up the if statement, but really I cannot figure it out.

  • `print("Yes)` should be replaced with `print("Yes")` and also have a look at the answer below to get the correct algorithm – Sheldore Sep 03 '18 at 11:43

3 Answers3

1

n is divisible by k if the modulo is zero. So, your condition should be if user_number % 3 == 0.

blue_note
  • 27,712
  • 9
  • 72
  • 90
0

It should be print("Yes") instead of print("Yes)

Sparsh
  • 1
  • 3
0
user_input = input("Type an integer: ")
user_int = int(user_input)
if user_int % 3==0:
    print("Yes")
else:
    print("No")
  1. print("Yes")

  2. Use modulo operator

khelwood
  • 55,782
  • 14
  • 81
  • 108
wins999
  • 1,432
  • 1
  • 9
  • 10