Practice Python Exercise 2

  • #### Ask the user for a number. Depending on whether the number is even or odd, print out an appropriate message to the user. Hint: how does an even / odd number react differently when divided by 2?

Extras:

  • If the number is a multiple of 4, print out a different message.
  • Ask the user for two numbers: one number to check (call it num) and one number to divide by (check). If check divides evenly into num, tell that to the user. If not, print a different appropriate message.

In [ ]:
#Main Solution without extras

def main():
    if( (int(input("Enter a number"))) % 2 == 0):
        print("Even number")
    else:
        print("Odd number")
main()

In [1]:
#Implementing Extras 1

def main():
    number = get_number()
    if(number  % 2 == 0):
        print("Even number")
        if(number % 4 == 0):
           print("Even number and multiple of 4")
    else:
        print("Odd number")
def get_number():
       return int(input("Enter a number"))
main()


Enter a number4
Even number
Even number and multiple of 4

In [10]:
#Implementing Extras 2

def main():
    number = get_number() 
    if(number  % 2 == 0):
        print("Even number")
        if(number % 4 == 0):
           print("Even number and multiple of 4")
    else:
        print("Odd number")
    number2,number3= get_number() , get_number()
    if (number2 % number3 == 0):
        print("{} evenly devides {}".format(number2,number3))
    else:
        print("{} not evenly devides by {}".format(number2,number3))
def get_number():
       return int(input("Enter a number"))
main()


Enter a number4
Even number
Even number and multiple of 4
Enter a number3
Enter a number2
3 not evenly devides by 2

In [ ]: