-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalc.py
More file actions
60 lines (54 loc) · 2.11 KB
/
calc.py
File metadata and controls
60 lines (54 loc) · 2.11 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
# Module by @DeBotMod
from telethon import events
from userbot import client
info = {'category': 'tools', 'pattern': '.calc', 'description': 'калькулятор обыкновенный. Вид: .calc [n+m],'
' .calc [n-m], .calc [n^m], .calc [n*m], .calc [n/m]'}
@client.on(events.NewMessage(pattern=r'^.calc'))
async def handle_calculator(event):
try:
message = event.message.message
formula = message.split()[1:]
expression = ''.join(formula)
if '+' in expression:
operands = expression.split('+')
operator = '+'
elif '-' in expression:
operands = expression.split('-')
operator = '-'
elif '*' in expression:
operands = expression.split('*')
operator = '*'
elif '/' in expression:
operands = expression.split('/')
operator = '/'
elif '^' in expression:
operands = expression.split('^')
operator = '^'
else:
await event.edit('Неподдерживаемая операция!')
return
operands = [float(operand) for operand in operands]
result = None
if operator == '+':
result = sum(operands)
elif operator == '-':
result = operands[0] - sum(operands[1:])
elif operator == '*':
result = 1
for operand in operands:
result *= operand
elif operator == '/':
if 0 in operands:
await event.edit('На ноль делить нельзя!')
return
result = operands[0]
for operand in operands[1:]:
result /= operand
elif operator == '^':
result = operands[0]
for exponent in operands[1:]:
result **= exponent
result = round(result)
await event.edit(f'Результат: <b>{result}</b>', parse_mode='HTML')
except Exception as e:
await event.edit(f'Произошла ошибка: {str(e)}')