0

This is a freecodecamp exercise in the scientific Python course. Quick note, this program is still not complete per the submission guidelines. After running this program, the output should be the calculation of all entries in the list, but the calculation only happens for the first equation.

import random

def exception_handling(num1, num2, operator):
    try:
        int(num1)
    except:
        return "Error: Numbers must only include digits"

    try:
        int(num2)
    except:
        return "Error: Numbers must only include digits"

    try:
        if len(num1)>4 or len(num2)>4:
            raise BaseException
    except:
        return "Error: Numbers cannot be more than four digits"

    try:
        if operator != '+' and operator != '-':
            raise BaseException
    except:
        return "Error: Operator must be '+' or '-'."


def arithmetic_arranger(problems):
    try:
        if len(problems) > 5:
            raise BaseException
    except:
        return "Error too many problems"

    for i in problems:
        problem = i.split()
        print(problem)
        num1 = problem[0]
        num2 = problem[2]
        operator = problem[1]
        exp = exception_handling(num1, num2, operator)

        if exp != '':
            if operator == "+":
                result = int(num1) + int(num2)
            elif operator == "-":
                result = int(num1) - int(num2)
            return result
        else:
            return exp


print(arithmetic_arranger(["555 + 444", "557 - 435"]))

This is the program I wrote. The expected output should be the results from both equations provided in the function call arrithmetic_arranger. However, actual output is:

['555', '+', '444']
999
Christopher Peisert
  • 21,862
  • 3
  • 86
  • 117
Abhirajs
  • 1
  • 2
  • 2
    There is a `return` statement at the end of the `for i in problems` loop. `return` makes the function exit immediately, so the loop only covers the first problem. – John Gordon Apr 02 '22 at 00:17

2 Answers2

0

You are returning rather on the first iteration. Perhaps you would like to print the values?

def arithmetic_arranger(problems):
    try:
        if len(problems) > 5:
            raise BaseException
    except:
        return "Error too many problems"

    for i in problems:
        problem = i.split()
        print(problem)
        num1 = problem[0]
        num2 = problem[2]
        operator = problem[1]
        exp = exception_handling(num1, num2, operator)
        if exp != '':
            if operator == "+":
                result = int(num1) + int(num2)
            elif operator == "-":
                result = int(num1) - int(num2)
            # This was returning
            print(result)
        else:
            # So was this
            print(exp)

Or, you could simply save the results to a list results and return them at the end

def arithmetic_arranger(problems):
    # Save them to this list
    results = []
    try:
        if len(problems) > 5:
            raise BaseException
    except:
        return "Error too many problems"

    for i in problems:
        problem = i.split()
        print(problem)
        num1 = problem[0]
        num2 = problem[2]
        operator = problem[1]
        exp = exception_handling(num1, num2, operator)
        if exp != '':
            if operator == "+":
                result = int(num1) + int(num2)
            elif operator == "-":
                result = int(num1) - int(num2)
            # By appending here
            results.append(result)
        else:
            results.append(exp)
    # And returning at the bottom 
    return results

Output

['555', '+', '444']
['557', '-', '435']
[999, 122]

The return keyword just returns a value (or no value) to the caller, and ends the function.


Edit for comments,

Updated conditionals:

if exp != '':
            if operator == "+":
                result = int(num1) + int(num2)
            elif operator == "-":
                result = int(num1) - int(num2)
            elif operator == "*":
                result = int(num1) * int(num2)
            elif operator == "/":
                result = int(num1) / int(num2)
            # By appending here
            results.append(result)
['555', '*', '444']
['557', '/', '435']
[246420, 1.2804597701149425]
Freddy Mcloughlan
  • 4,129
  • 1
  • 13
  • 29
  • Thank you much about that explanation!! Really appreciate you helping me out. One question though, if i change the operator symbol to * or /, in the second code that you mentioned, the program does not give me the error it is supposed to, but just does the calculation of the second equation and return in the list. any ideas why could that be? – Abhirajs Apr 02 '22 at 18:03
  • No worries, maybe because you don't have a condition for those operators? `if operator == "*"`? – Freddy Mcloughlan Apr 03 '22 at 03:40
  • i had that condition under the other function exception_handling, and that function was called as the variable exp in the code. – Abhirajs Apr 03 '22 at 16:09
  • Updated my answer with the if statements – Freddy Mcloughlan Apr 03 '22 at 23:20
0
# Accumulate results. 
def exception_handling(num1, num2, operator):
    try:
        int(num1)
    except:
        return "Error: Numbers must only include digits"

    try:
        int(num2)
    except:
        return "Error: Numbers must only include digits"

    try:
        if len(num1) > 4 or len(num2) > 4:
            raise BaseException
    except:
        return "Error: Numbers cannot be more than four digits"

    try:
        if operator != '+' and operator != '-':
            raise BaseException
    except:
        return "Error: Operator must be '+' or '-'."


def arithmetic_arranger(problems):
    result= [] # Accumulate resutls as a list
    try:
        if len(problems) > 5:
            raise BaseException
    except:
        return "Error too many problems"

    for i in problems:
        problem = i.split()
        print(problem)
        num1 = problem[0]
        num2 = problem[2]
        operator = problem[1]
        exp = exception_handling(num1, num2, operator)
        if exp != '':
            if operator == "+":
                result.append(int(num1) + int(num2))
            elif operator == "-":
                result.append(int(num1) + int(num2))
        else:
            return exp
    return result

print(arithmetic_arranger(["555 + 444", "557 - 435"]))

Output

['555', '+', '444']
['557', '-', '435']
[999, 992]
Carl_M
  • 861
  • 1
  • 7
  • 14