-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuild an Arithmetic Formatter Project.py
More file actions
60 lines (40 loc) · 1.55 KB
/
Copy pathBuild an Arithmetic Formatter Project.py
File metadata and controls
60 lines (40 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
def arithmetic_arranger(problems, show_answers=False):
if len(problems)>5:
return "Error: Too many problems."
top_row = ""
bottom_row = ""
lines = ""
results_row = ""
for i,problem in enumerate(problems):
parts=problem.split()
if len(parts)>3:
return "Error: Invalid problem format."
num1,operator,num2 = parts
if operator not in ['+','-']:
return "Error: Operator must be '+' or '-'."
if not (num1.isdigit() and num2.isdigit()):
return "Error: Numbers must only contain digits."
if len(num1)>4 or len(num2)>4:
return "Error: Numbers cannot be more than four digits."
if show_answers:
if operator == "+":
result = str(int(num1)+int(num2))
else:
result = str(int(num1) - int(num2))
width = max(len(num1), len(num2)) + 2
if i > 0:
top_row += " "
bottom_row += " "
lines += " "
results_row += " "
top_row += num1.rjust(width)
bottom_row += operator + num2.rjust(width - 1)
lines += "-" * width
if show_answers:
results_row += result.rjust(width)
if show_answers:
return f"{top_row}\n{bottom_row}\n{lines}\n{results_row}"
else:
return f"{top_row}\n{bottom_row}\n{lines}"
return problems
print(f'\n{arithmetic_arranger(["32 + 698", "30a01 - 2", "45 + 43", "123 + 49"])}')