Control Structures (Number Examiner)

Back To The Index

examine_numbers

Control Structures : examine_numbers

In this program we will learn making an interactive program. At the start of execution program asks user to supply a number, and then tests it to find if its a multiple of 2 or 3 or None and prints this information out. User can enter any non numeric value to have program terminate.

In [3]:
###################################################
#  Filename : examine_numbers                     #
#  Purpose : To demonstrate console input and     #
#   usage of the following control structures     #
#            1. if elif else                      #
#            2. while loop                        #
#  Author : Niel S.                               #
#  The English Tea Company LLC                    #
###################################################

#The main function: It drives the program.
def main():
    
    #Prompt user to enter a number. Store the input 
    # and identify it with x
    x = input("Enter a positive number: ")
    
    #Test if 'x' is numeric, if yes then enter loop else go to statement after loop.
    while (x.isnumeric()):
        x = int (x)
        
        if (x%2==0 and x%3==0):
            print ('The number ',x,' is a multiple of both 2 and 3.')
        elif x%2 == 0:
            print ('The number ',x,' is a multiple of 2 but not 3')
        elif x%3 == 0:
            print ('The number ',x,' is a multiple of 3 but not 2')
        else:
            print ('The number ',x,' is a not a multiple of either 2 or 3')
        # Get the value of x for next iteration. Enter any non numeric value to
        #  exit the program.
        x = input("Enter a positive number: ")
        
    #Final statement. Bid user a polite good bye   
    print ('Good Bye')

if __name__ == '__main__':
    main()
Enter a positive number: 2
The number  2  is a multiple of 2 but not 3
Enter a positive number: 3
The number  3  is a multiple of 3 but not 2
Enter a positive number: 6
The number  6  is a multiple of both 2 and 3.
Enter a positive number: 7
The number  7  is a not a multiple of either 2 or 3
Enter a positive number: I AM DONE
Good Bye

Back To The Index