58.4k views
1 vote
Define a function that creates a list of all the numbers that are factors of the user's number. For example, if the function is called factor, factor(36) should return [1, 2, 3, 4, 6, 9, 12, 18, 36]. The numbers in your list should be sorted from least to greatest, and 1 and the original number should be included.

Remember to consider negative numbers as well as 0.

User Nkongara
by
6.3k points

1 Answer

4 votes

Answer:

#function that return all the factor of a number

def factor(num):

#create an empty list

lst=[]

#if input is positive

if num>0:

for i in range(1,num+1):

if num%i==0:

lst.append(i)

#if input is negative

elif num<=0:

for i in range(num,0):

if num%i==0:

lst.append(i)

# return the list of all factors

return lst

# read the number from user

x = int(input ( "Enter the number:: " ))

print("factors of the number are:")

# call the function with parameter x

print(factor(x))

print('\\')

Step-by-step explanation:

Read a number from user. Call the function "factor()" with parameter "x". In the function create an empty list. if the input is positive then check all the number from 1 to input. If a number divides the input then add it into list.If input is negative then check all the number from input to 0. if a number divides the input then add it to the list. After that return the list.

#Output:

Enter the number:: 36

factors of the number are:

[1, 2, 3, 4, 6, 9, 12, 18, 36]

Enter the number:: -16

factors of the number are:

[-16, -8, -4, -2, -1]

User Jebastin J
by
5.9k points