Asks the user for a number and then prints out a list of all the divisors of that number - Python:
#create a function that asks the user for a number and then prints out a list of all the divisors of that number
def divisors(num):
"""
Prints out all the divisors of a number.
Parameters
----------
num : int
Number to find divisors of.
Returns
-------
list
List of all the divisors of ``num``.
"""
#create an empty list to store the divisors in
divisor_list = []
#loop through all numbers from 1 to the number entered by the user
for i in range(1, num + 1):
#if the number is evenly divisible by i, add i to the list of divisors
if num % i == 0:
divisor_list.append(i)
#return the list of divisors
return divisor_list
#example
result = divisors(12)
#output
print(result) #[1, 2, 3, 4, 6, 12]
Top comments (0)