forked from talkpython/100daysofcode-with-python-course
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFreecodecamp_calcul_project
More file actions
104 lines (91 loc) · 4.85 KB
/
Freecodecamp_calcul_project
File metadata and controls
104 lines (91 loc) · 4.85 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# Калькулятор для обработки примеров на сложение и вычитание целочисленных типов данных
# Примеры подаются в виде списка. Не более 4 шт.
# Размер числа не превышает 4 цифр
def arithmetic_arranger(list_of_problems):
# Функция, которая сбрасывает все вычисления. В случае обнаружения ошибки
def reset():
global line_1, line_2, line_3, line_4
line_1 = ''
line_2 = ''
line_3 = ''
line_4 = ''
# Проверка количества примеров (не больше 4)
not_checked = True
while not_checked:
if len(list_of_problems) > 4:
print('Error: Too many problems.')
not_checked = False
else:
not_checked = False
# Создание строк для записи примеров столбиком
global line_1, line_2, line_3, line_4
line_1 = ''
line_2 = ''
line_3 = ''
line_4 = ''
# Цикл решения примеров
for problem in list_of_problems:
# Решение примера на сложение
if '+' in problem:
a,b = problem.split('+') # Выделение слагаемых
lenth = len(a)+1 if len(a)>len(b) else len(b)+1 # Определение ширины примера
try:
a,b = int(a),int(b) # Проверка на отсутствие букв и символов в числах
except:
print('Error: Numbers must only contain digits.')
reset()
break
# Проверка размера числа (не больше 4 цифр)
if len(str(a))>4 or len(str(b))>4:
print('Error: Numbers cannot be more than four digits.')
reset()
break
# Формирование примера в форме столика
line_1 += ' '*(lenth-len(str(a)))+str(a)+' '*4
line_2 += '+' + ' '*(lenth-len(str(b))-1)+str(b)+' '*4
line_3 += '-'*lenth+' '*4
c = int(a) + int(b)
line_4 += ' '*(lenth-len(str(c)))+str(c)+' '*4
# Решение примера на вычетание
elif '-' in problem:
a,b = problem.split('-') # Выделение уменьшаемого и вычитаемого
lenth = len(a)+1 if len(a)>len(b) else len(b)+1 # Определение ширины примера
try:
a,b = int(a),int(b) # Проверка на отсутствие букв и символов в числах
except:
print('Error: Numbers must only contain digits.')
reset()
break
# Проверка размера числа (не больше 4 цифр)
if len(str(a))>4 or len(str(b))>4:
print('Error: Numbers cannot be more than four digits.')
reset()
break
# Формирование примера в форме столика
line_1 += ' '*(lenth-len(str(a)))+str(a)+' '*4
line_2 += '-' + ' '*(lenth-len(str(b))-1)+str(b)+' '*4
line_3 += '-'*lenth+' '*4
c = int(a) - int(b)
line_4 += ' '*(lenth-len(str(c)))+str(c)+' '*4
# Обработка случая использования иных знаков кроме + и -
else:
print("Error: Operator must be '+' or '-'")
reset()
break
# Отмена печати примеров, если в каком-то из них ошибка
if len(line_1) == 0:
pass
else:
# Печать примеров
print(f'{line_1}\n{line_2}\n{line_3}\n{line_4}')
# Тестирование функции
arithmetic_arranger(["32 + 698", "4531 - 2", "45 + 43", "123 + 49", "123 + 49"])
print()
arithmetic_arranger(["32 + 8", "1 - 3801", "9999 + 9999", "523 - 49"])
print()
arithmetic_arranger(["32 + 698", "4531 - 2", "45 + 43", "123 + 445654649"])
print()
arithmetic_arranger(["32 + 698", "4531 - 2", "4wer5 + 43", "123 + 49"])
print()
arithmetic_arranger(["32 + 698", "4531 / 2", "45 + 43", "123 + 49"])
print()