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