0
def addition(number, number2, mathematic_assignment):
    sum_value = number + number2
    return f'The sum of {number} {mathematic_assignment} {number2} is {sum_value}'


def subtraction(number, number2, mathematic_assignment):
    subtract_value = number - number2
    return f'The subtraction of {number} {mathematic_assignment} {number2} is {subtract_value}'


def multiply(number, number2, mathematic_assignment):
    multiplied_value = number * number2
    return f'The result of multiplying {number} {mathematic_assignment} {number2} is {multiplied_value}'


def division(number, number2, mathematic_assignment):
    divided_value = number / number2
    return f'The division of {number} {mathematic_assignment} {number2} is {round(divided_value)}'


def run_program():
    print("Welcome to the base calculator")
    is_running = True
    while is_running:
        first_number = int(input('Please provide the first number: '))
        second_number = int(input('Please provide the second number: '))
        mathematic_assignment = input('Please provide mathematical preference: \n+\n-\n*\n/\n')
        if mathematic_assignment == '+':
            print(addition(first_number, second_number, mathematic_assignment))
        elif mathematic_assignment == '-':
            print(subtraction(first_number, second_number, mathematic_assignment))
        elif mathematic_assignment == '*':
            print(multiply(first_number, second_number, mathematic_assignment))
        elif mathematic_assignment == '/':
            print(division(first_number, second_number, mathematic_assignment))
        else:
            print('There can be an error in the output, you must only put in "+", "-", "*", "/"')

        choice = input("Press 'q' to quit or anything to go on: ")
        if choice == 'q':
            print('The program will now quit')enter code here
            is_running = False


if __name__ == '__main__':
    run_program()enter code here

The following code is an example of a base calculator made out of functions. However, the problem I have been struggling with is starting the calculator where the first number is the previous result, so you only get the second number and the mathematical operation required (so if 5+5 is 10 then the next time you run the code (using the while loop) now the first number is 10 + {another number}).

As you can see it's all ready except the following challenge. I am more than sure I can tackle this on my own without the functions however I want to know how functions operate and learn them in more detail. I would really appreciate an answer

azro
  • 53,056
  • 7
  • 34
  • 70
SLIME SHADY
  • 26
  • 1
  • 5

1 Answers1

1

First the computing method should return the numeric value, so you can use it later, I removed parameter mathematic_assignment as every method knows its own

def addition(number, number2):
    sum_value = number + number2
    return sum_value, f'The sum of {number} + {number2} is {sum_value}'
def subtraction(number, number2):
    subtract_value = number - number2
    return subtract_value, f'The subtraction of {number} - {number2} is {subtract_value}'
def multiply(number, number2):
    multiplied_value = number * number2
    return multiplied_value, f'The result of multiplying {number} * {number2} is {multiplied_value}'
def division(number, number2):
    divided_value = number / number2
    return divided_value, f'The division of {number} / {number2} is {round(divided_value)}'

Then in the main code, you may

  • save the result of each call
  • ask whether to use or not the previous result, if it exists (you could also not ask and use it automatically if it exists)
def run_program():
    numeric_result = None
    print("Welcome to the base calculator")
    while True:
        result = None
        if numeric_result:
            first_number = input(
                f'Please provide the first number or empty to use the previous result ({numeric_result}):')
            first_number = int(first_number) if first_number else numeric_result
        else:
            first_number = int(input('Please provide the first number: '))

        second_number = int(input('Please provide the second number: '))
        mathematic_assignment = input('Please provide mathematical preference: + - * / : ')

        if mathematic_assignment == '+':
            result = addition(first_number, second_number)
        elif mathematic_assignment == '-':
            result = subtraction(first_number, second_number)
        elif mathematic_assignment == '*':
            result = multiply(first_number, second_number)
        elif mathematic_assignment == '/':
            result = division(first_number, second_number)
        else:
            print('There can be an error in the output, you must only put in "+", "-", "*", "/"')

        if result:
            numeric_result, str_result = result
            print(str_result)

        choice = input("Press 'q' to quit or anything to go on: ")
        if choice == 'q':
            print('The program will now quit')
            break

Here's a sample run

Welcome to the base calculator
Please provide the first number: 5
Please provide the second number: 5
Please provide mathematical preference: + - * / : +
The sum of 5 + 5 is 10
Press 'q' to quit or anything to go on:
Please provide the first number or empty to use the previous result (10):
Please provide the second number: 5
Please provide mathematical preference: + - * / : *
The result of multiplying 10 * 5 is 50
Press 'q' to quit or anything to go on:
Please provide the first number or empty to use the previous result (50):12
Please provide the second number: 12
Please provide mathematical preference: + - * / : -
The subtraction of 12 - 12 is 0
Press 'q' to quit or anything to go on: q
The program will now quit
azro
  • 53,056
  • 7
  • 34
  • 70