-
Notifications
You must be signed in to change notification settings - Fork 0
/
arithmetic_arranger.py
71 lines (58 loc) · 1.66 KB
/
arithmetic_arranger.py
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
def arithmetic_arranger(problems, show = False):
qst_1 = ''
qst_2 = ''
equal = ''
ans = ''
semua = ''
loop = 1
if len(problems) > 5:
return 'Error: Too many problems.'
for numbers in problems:
number = numbers.split()
if not number[1] == '-':
if not number[1] == '+':
return 'Error: Operator must be \'+\' or \'-\'.'
try:
int_1 = int(number[0])
int_2 = int(number[2])
except:
return 'Error: Numbers must only contain digits.'
if len(number[0]) > 4 or len(number[2]) > 4:
return 'Error: Numbers cannot be more than four digits.'
if len(number[0]) >= len(number[2]):
equal_len = len(number[0]) + 2
else:
equal_len = len(number[2]) + 2
space_1 = equal_len - len(number[0])
space_2 = equal_len - len(number[2]) - 1
for i in range(space_1):
qst_1 += ' '
qst_1 += number[0]
if loop < len(problems):
qst_1 += ' '
qst_2 += number[1]
for i in range(space_2):
qst_2 += ' '
qst_2 += number[2]
if loop < len(problems):
qst_2 += ' '
for i in range(equal_len):
equal += '-'
if loop < len(problems):
equal += ' '
if show:
if number[1] == '+':
tmp = int(number[0]) + int(number[2])
if number[1] == '-':
tmp = int(number[0]) - int(number[2])
for i in range(equal_len-len(str(tmp))):
ans += ' '
ans += str(tmp)
if loop < len(problems):
ans += ' '
loop += 1
if show == True:
semua = qst_1 + '\n' + qst_2 + '\n' + equal + '\n' + ans
else:
semua = qst_1 + '\n' + qst_2 + '\n' + equal
return semua