-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex45.py
More file actions
68 lines (60 loc) · 2.02 KB
/
Copy pathex45.py
File metadata and controls
68 lines (60 loc) · 2.02 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
# 1) Decorar o exercício da Aula de Nested Functions e executar as três funções.
# - Na função que executa a string maiúscula use um decorador para imprimir:
# ’Estou gritando’.
# - Na função que soma dois números decore com ‘O maior entre os dois é:
# {maior}’.
# - Na função que soma um número com outro aleatório decore com ‘Somando
# com o aleatório:'
# - Faça o tratamento com Wraps do programa.
from random import randint
from functools import wraps
global aleatorio
aleatorio = randint(0, 15)
def decorando(funcao):
"""
função decoradora
"""
print(f'{decorando.__doc__}')
@wraps(funcao)
def wrapper(*args):
if args[0] == 'upper':
print('--> EU ESTOU GRITANDO: ', end='')
return funcao(args[0], args[1].upper())
elif args[0] == 'soma_num':
if args[1] > args[2]:
print(f'O maior numero é: {args[1]}')
else:
print(f'--> O maior numero é {args[2]}')
return funcao(*args)
elif args[0] == 'soma_aleatoria':
global aleatorio
print(f'--> O numero aleatório é: {aleatorio}')
return funcao(*args) + aleatorio
else:
def erro():
print('Não existe função chamada')
return erro
return wrapper
@decorando
def resposta(op, *args):
"""
função que retorna a resposta decorada
"""
print(f'{resposta.__doc__}')
if op == 'upper':
def upper():
print(args[0])
return upper()
elif op == 'soma_num':
def soma_num(*args):
print(f'{args[0]} + {args[1]} = {args[0] + args[1]}')
return soma_num(args[0], args[1])
elif op == 'soma_aleatoria':
def soma_aleatoria():
global aleatorio
print(f'{args[0]} + {aleatorio} = {args[0] + aleatorio}')
return args[0] + aleatorio
return soma_aleatoria()
resposta('upper', 'eu te amo')
resposta('soma_num', 13, 19)
resposta('soma_aleatoria', 19)