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

45 lines
789 B
Python
Raw Permalink 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-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
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
2022-10-15 22:17:04 +00:00
# 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'
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()