0

How would I go about determining if an integer is a multiple of 2 but not a multiple of 3 print ‘ is a multiple of 2 only.’ ? Using python.

if myint%2: 
    print(str(myint), "is a multiple of 2 only")

How do I get it to output "but not a multiple of 3"

keepAlive
  • 6,369
  • 5
  • 24
  • 39
Aaron
  • 9
  • 2

2 Answers2

0

What about doing

your_number%2 == 0 and your_number%3 != 0

Where % is the modulus operator and divides left hand operand by right hand operand and returns the remainder. So if the remainder is equal to 0, the left operand is a multiple of the right operand.

Thus

your_number%2 == 0 returns True if your_number is a multiple of 2

and

your_number%3 != 0 returns True if your_number is not a multiple of 3.

To provide you with a full answer, what you need is:

if myint%2 == 0 and myintr%3 != 0:
    print(str(myint), "is a multiple of 2 and not a multiple of 3")
keepAlive
  • 6,369
  • 5
  • 24
  • 39
0

This is how you can do it:

#first checks if myint / 2 doesn't has a reminder
#then checks if myint / 3 has a reminder
if not myint % 2 and myint % 3: 
    print(myint,"is a multiple of 2 only")

or if you want:

if myint % 2 == 0 and myint % 3 != 0: 
    print(myint,"is a multiple of 2 only")

Both work the same

Taku
  • 31,927
  • 11
  • 74
  • 85
  • Your True/False evaluation style is a [*don't* for Python Program at Google](https://google.github.io/styleguide/pyguide.html?showone=True/False_evaluations#True/False_evaluations). I mean your `if not myint % 2`. – keepAlive Feb 26 '17 at 23:22
  • there isn't anything wrong with it – Taku Feb 26 '17 at 23:27