-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathch002_highest_digit.py
More file actions
54 lines (43 loc) · 1.49 KB
/
ch002_highest_digit.py
File metadata and controls
54 lines (43 loc) · 1.49 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
"""
Create a function named highest_digit that accepts a numerical argument and returns the highest digit in that number.
For instance,
highest_digit(379) should return 9
highest_digit(2) should return 2
highest_digit(377401) should return 7
"""
# Aprendi o novo comando max(...)
# Corrigi o erro que ocorria ao usar `for x in n` sem converter n para string com str(n).
def highest_digit(n: int) -> int:
"""
DOCSTRING:
Retorna o maior dígito presente em um número inteiro positivo.
EXEMPLO:
>>> highest_digit(1654)
6
"""
return max(int(x) for x in str(n))
if __name__ == "__main__":
print("---------- Highest Digit ----------")
print("Digite um número inteiro positivo.")
print("Escreva 'sair' para encerrar ou 'help' para ajuda.\n")
while True:
texto = input("> ").strip().lower()
if texto == "sair":
print("\nEncerrando...\n")
break
if texto == "help":
print(highest_digit.__doc__)
continue
if not texto:
continue
# Verificar se é um número inteiro positivo
try:
numero = int(texto)
if numero < 0:
print("❌ Por favor, digite um número inteiro positivo.\n")
continue
except ValueError:
print("❌ Digite apenas um número inteiro positivo, 'help' ou 'sair'.\n")
continue
resultado = highest_digit(numero)
print("Resultado:", resultado, "\n")