Single list comprehension | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

Single list comprehension

I have been stuck on this question for days. I'm not sure what exactly is needed to solve this problem. Do I need to create a for loop and multiply it by 7? Do I need to delete the pass and add the print (multiples_of(7, 5)) # [7, 14, 21, 28, 35] with the class? def multiples_of(number, length): pass print (multiples_of(7, 5)) # [7, 14, 21, 28, 35]

3rd Jan 2023, 3:50 AM
Chris
Chris - avatar
5 Respostas
+ 3
You make it seems so simple. Thank you!
3rd Jan 2023, 4:43 AM
Chris
Chris - avatar
+ 2
def multiples_of(number, length): list = [i*number for i in range(1, length + 1)] return list print(multiples_of(7,5)) ''' Ok, let me explain. At first, I defined a function named multiples_of (šæš‘’š‘Žš‘Ÿš‘› š‘šš‘œš‘Ÿš‘’ š‘Žš‘š‘œš‘¢š‘” "š‘­š’–š’š’„š’•š’Šš’š’š’”" š‘–š‘› PĶŸyĶŸtĶŸhĶŸoĶŸnĶŸ CĶŸoĶŸrĶŸeĶŸ ŹŸį“‡ssį“É“ 63.1) def multiples_of It has 2 arguments (number and length) def multiples_of(number, length) Inside the function I created a list. list = [i*number for i in range(1, length + 1)] # This list is a shortcut to the following code: list = [] for i in range(1, length+1): list.append(i*number) (šæš‘’š‘Žš‘Ÿš‘› š‘šš‘œš‘Ÿš‘’ š‘Žš‘š‘œš‘¢š‘” "š‘­š’š’“ š’š’š’š’‘š’”" š‘–š‘› PĶŸyĶŸtĶŸhĶŸoĶŸnĶŸ CĶŸoĶŸrĶŸeĶŸ ŹŸį“‡ssį“É“ 27.1) Now to get this list I used return list ā„š•’š•”š•”š•Ŗ ā„‚š• š••š••š•šš•Ÿš•˜ ć‹” '''
3rd Jan 2023, 3:58 AM
Fź«€ā²…įƒ«į„†ĶŸį„™į„‰įƽ
Fź«€ā²…įƒ«į„†ĶŸį„™į„‰įƽ - avatar
+ 2
Brit you can let range() generate the multiples, and then convert the range to a list. Here is how: Start the range at number and add a step value of number until it exceeds number*length. The step value is the third parameter in range(start, end, step). Finally, use the list() function to convert the range type into a list type. def multiples_of(number, length): return list(range(number, number*length + 1, number)
3rd Jan 2023, 9:59 PM
Brian
Brian - avatar
+ 1
Ipang Thanks for your comment šŸ™‚. I added the explanation to my answer.
3rd Jan 2023, 8:58 AM
Fź«€ā²…įƒ«į„†ĶŸį„™į„‰įƽ
Fź«€ā²…įƒ«į„†ĶŸį„™į„‰įƽ - avatar
0
Brit Welcome šŸ¤—
3rd Jan 2023, 8:56 AM
Fź«€ā²…įƒ«į„†ĶŸį„™į„‰įƽ
Fź«€ā²…įƒ«į„†ĶŸį„™į„‰įƽ - avatar