-
Notifications
You must be signed in to change notification settings - Fork 0
/
arithmetic_arranger
83 lines (73 loc) · 2.65 KB
/
arithmetic_arranger
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
def arithmetic_arranger(problems,answer = False):
arranged_problems = []
#too many problems.
if len(problems) > 5 :
return("Error: Too many problems.")
#operator must be "+" or "-".
for problem in problems :
operand1,operator,operand2 = problem.split()
if operator not in "-+" :
return("Error: Operator must be '+' or '-'.")
#operands must only be digits.
if operand1.isdigit() is True and operand2.isdigit() is True :
None
else :
return("Error: Numbers must only contain digits.")
#too many digits.
result1 = len(operand1) <= 4
result2 = len(operand2) <= 4
if result1 is True and result2 is True :
None
else :
return("Error: Numbers cannot be more than four digits.")
width = max(len(operand1),len(operand2)) + 2
#formating each line.
line1 = f"{operand1.rjust(width)}"
line2 = f"{operator}{operand2.rjust(width-1)}"
line3 = "-" * width
#arranging the problems.
if answer is False :
try:
arranged_problems[0] += (" " * 4)+ line1
except IndexError:
arranged_problems.append(line1)
try:
arranged_problems[1] += (" " * 4)+ line2
except IndexError :
arranged_problems.append(line2)
try:
arranged_problems[2] += (" " * 4)+ line3
except IndexError :
arranged_problems.append(line3)
#return(f"{arranged_problems[0]}\n{arranged_problems[1]}\n{arranged_problems[2]}")
elif answer is True :
if operator == "+" :
solution = int(operand1) + int(operand2)
solution = str(solution)
else :
solution = int(operand1) - int(operand2)
solution = str(solution)
line4 = f"{solution.rjust(width)}"
try:
arranged_problems[0] += (" " * 4)+ line1
except IndexError:
arranged_problems.append(line1)
try:
arranged_problems[1] += (" " * 4)+ line2
except IndexError :
arranged_problems.append(line2)
try:
arranged_problems[2] += (" " * 4)+ line3
except IndexError :
arranged_problems.append(line3)
try:
arranged_problems[3] += " " * 4 + line4
except IndexError :
arranged_problems.append(line4)
#print(arranged_problems)
#return(f"{arranged_problems[0]}\n{arranged_problems[1]}\n{arranged_problems[2]}\n{arranged_problems[3]}")
if answer is False :
return(f"{arranged_problems[0]}\n{arranged_problems[1]}\n{arranged_problems[2]}")
else :
return(f"{arranged_problems[0]}\n{arranged_problems[1]}\n{arranged_problems[2]}\n{arranged_problems[3]}")
#print(arithmetic_arranger(["32 + 8","1 - 3801","9999 + 9999","523 - 49"]))