Question
Write a function factors(), which takes a single positive integer (named n) as its argument. This function returns a list that contains all of the
Write a function factors(), which takes a single positive integer (named n) as its argument. This function returns a list that contains all of the integer factors of the argument (including 1 and the number itself), and only those values, in descending order. For example, factors(24) would return the list [24, 12, 8, 6, 4, 3, 2, 1].
If n is not positive, the function simply returns [], which is the empty list. That is, you will need to write an if-statement to check for an invalid value for n, and if n is indeed invalid, your code will return [].
To add an item to the end of the list we use the append method. Here is an example:
items = [] # create an empty list
# ...other code here ...
items.append(x) # where x is the value or variable to want to append
Examples:
Function Arguments Return Value 29 100 1024 (100, 50, 25, 20, 10, 5, 4, 2, 11 [100, 50, 25, 20, 10, 5, 4, 2, 1] 9 [1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 11Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started