uaveiro-leci/1ano/1semestre/fp/aula04/divs-cate.py

41 lines
793 B
Python
Raw Normal View History

2022-10-15 22:17:04 +00:00
# Função principal
2022-10-14 22:03:25 +00:00
def main():
n = int(input('Introduza um número: '))
div_list_array = divList(n)
2022-10-15 22:17:04 +00:00
2022-10-18 17:18:56 +00:00
div_list = ", ".join(div_list_array)
2022-10-15 22:17:04 +00:00
2022-10-18 17:18:56 +00:00
print(f"""
2022-10-14 22:03:25 +00:00
--------------------
2022-10-18 17:18:56 +00:00
Número introduzido: {n}
2022-10-14 22:03:25 +00:00
Lista de divisores:
2022-10-18 17:18:56 +00:00
{div_list}
2022-10-14 22:03:25 +00:00
2022-10-18 17:18:56 +00:00
Este é um número {category(n, div_list_array)}.
2022-10-14 22:03:25 +00:00
--------------------
2022-10-18 17:18:56 +00:00
""")
2022-10-15 22:17:04 +00:00
# Obtém uma lista com todos os dividores de um número
def divList(n):
divs = []
for x in range(1, n):
if n % x == 0:
2022-10-18 17:18:56 +00:00
divs.append(str(x))
2022-10-15 22:17:04 +00:00
return divs
# Obtém a categoria de um número
def category(n, divs):
total = 0
for div in divs:
2022-10-18 17:18:56 +00:00
total += int(div)
2022-10-15 22:17:04 +00:00
if total < n: return 'deficiente'
if total == n: return 'perfeito'
2022-10-18 17:18:56 +00:00
if total > n: return 'abundante'
2022-10-14 22:03:25 +00:00
2022-10-18 17:18:56 +00:00
if __name__ == "__main__":
main()