Tell us what’s happening:
Arithmetic Formatter Project:
My output shows the same result like it should be. I have also checked for blank spaces with “repr()” function, which was mentioned in forum, but it still won’t work.
I would appreciate some help to learn, what is wrong in my code.
Your code so far
def arithmetic_arranger(problems, show_answers=False):
line_1 = ""
line_2 = ""
line_3 = ""
line_4 = ""
# Abfrage Bedingungen für Error - Anzahl Probleme
if len(problems) > 5:
raise ValueError('Error: Too many problems.')
for pro in problems:
first_number = pro.split()[0]
second_number = pro.split()[2]
operator = pro.split()[1]
# Abfrage Bedingungen für Error - Operator-Typ / nur Zahlen / max 4 Ziffern
if operator != '+' and operator != '-':
raise ValueError("Error: Operator must be '+' or '-'")
if first_number.isdigit() == False or second_number.isdigit() == False:
raise ValueError("Error: Numbers must only contain digits.")
if len(first_number) > 4 or len(second_number) > 4:
raise ValueError('Error: Numbers cannot be more than four digits.')
# Ermittlung max Länge der Nummern
max_number_length = len(str(max(int(first_number), int(second_number))))
# Lösen der Gleichung
if operator == '+':
result = int(first_number) + int(second_number)
elif operator == '-':
result = int(first_number) - int(second_number)
result = str(result)
# Bildung der Reihen
line_1 += f"{first_number:>{max_number_length+2}}{' '*4}"
line_2 += f"{operator}{second_number:>{max_number_length+1}}{' '*4}"
line_3 += f"{'-'* (max_number_length+2)}{' '*4}"
line_4 += f"{result:>{max_number_length+2}}{' '*4}"
# Anzeigen der Reihen
print(line_1.rstrip())
print(line_2.rstrip())
print(line_3.rstrip())
if show_answers == True:
print(line_4.rstrip())
return problems
#arithmetic_arranger(["3801 - 2", "123 + 49"])
#arithmetic_arranger(["1 + 2", "1 - 9380"])
#arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"])
#arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"])
#arithmetic_arranger(["3 / 855", "3801 - 2", "45 + 43", "123 + 49"])
#arithmetic_arranger(["24 + 85215", "3801 - 2", "45 + 43", "123 + 49"])
#arithmetic_arranger(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"])
#arithmetic_arranger(["3 + 855", "988 + 40"], True)
#arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project