Monday 12 October 2020

Part Three: Functions: Exercise 1: A Better Calculator App

In this exercise, we would put to use all the knowledge gained from the beginning to build a calculator app using functions. The calculator app would do the following:

·        It would have separate functions for addition, subtraction, multiplication and division. It should also find power, modulus and also perform integer division.

·        The app would take inputs from the user.

·        The app would ask which operation the user wants to perform.

·        The app would perform the selected operation and print the result out.

·        The app would ask the user if they have finished using the app or not.

·        Doscstings are provided for the functions used. Comments too where appropriate.

You can compare your code to mine below.

 

print('Simple Calculator App')

 

def add(x,y):

    """Add two numbers"""

    return x + y

def subtract(x,y):

    '''Subtract two numbers'''

    return x - y

def multiply(x,y):

    '''Multiply two numbers'''

    return x*y

def divide(x,y):

    '''Divide two numbers'''

    return x/y

 

def modulus(x,y):

    '''Divide two numbers to get modulus'''

    return x%y

def power(x,y):

    '''Power of two numbers'''

    return x**y

def integer_division(x,y):

    '''Integer division on two numbers'''

    return x//y

 

def check_if_user_has_finished():

    """

    Checks that the user wants to finish or not.

    Performs some verification of the input."""

    ok_to_finish = True

    user_input_accepted = False

    while not user_input_accepted:

        user_input = input('Do you want to finish (y/n): ')

        if user_input == 'y':

            user_input_accepted = True

        elif user_input == 'n':

            ok_to_finish = False

            user_input_accepted = True

        else:

            print('Response must be (y/n), please try again')

        return ok_to_finish

   

def get_operation_choice():

    input_ok = False

    while not input_ok:

        print('Menu Options are:')

        print('1. Add')

        print('2. Subtract')

        print('3. Multiply')

        print('4. Divide')

        print('5. Modulus')

         print('6. Power')

        print('7. Integer Division')

        print('-----------------')

        user_selection = input('Please make a selection: ')

        if user_selection in ('1', '2', '3', '4','5','6','7'):

            input_ok = True

        else:

            print('Invalid Input (must be 1 - 7)')

    print('-----------------')

    return user_selection

 

def get_numbers_from_user():

    num1 = float(input('Input the first number: '))

    num2 = float(input('Input the second number: '))

    return num1, num2

 

def get_integer_input(message):

    value_as_string = input(message)

    while not value_as_string.isnumeric():

        print('The input must be an integer')

        value_as_string = input(message)

    return int(value_as_string)

   

finished =False

while not finished:

    result = 0

    menu_choice = get_operation_choice()

    n1, n2 = get_numbers_from_user()

    if menu_choice == '1':

        result = add(n1, n2)

    elif menu_choice == '2':

        result = subtract(n1, n2)

    elif menu_choice == '3':

        result = multiply(n1, n2)

    elif menu_choice == '4':

        result = divide(n1, n2)

    elif menu_choice == '5':

        result = modulus(n1, n2)

    elif menu_choice == '6':

        result = power(n1, n2) 

    elif menu_choice == '7':

        result = integer_division(n1, n2)   

    #Get the operation from the user

    #Get the numbers from the user

    #Select the operation

    print('Result:', result)

    print('====================')

    finished = check_if_user_has_finished()

    #Determing if the user has finished

   

print('Bye')

 

 

 

No comments:

Post a Comment