Have a go at it. The editor and the docs panel are open, and your code is saved as you type. Running it needs a free account — you’ll come back to exactly what you wrote.
range and Counting Loops
When you want to loop a fixed number of times, or over positions rather than values, use range:
range(5) # 0, 1, 2, 3, 4 stops before 5 range(2, 6) # 2, 3, 4, 5 range(0, 10, 2) # 0, 2, 4, 6, 8 step of 2
Same rule as slicing: the stop value is not included. range(5) gives you five numbers starting at 0, which lines up exactly with the valid indices of a five-item list.
range does not build a list, it produces the numbers as you ask for them. list(range(3)) gives [0, 1, 2] if you actually need one.
Your task:
Write multiples_of(n, count) that returns a list of the first count positive multiples of n.
multiples_of(3, 4) returns [3, 6, 9, 12]. Note it starts at n, not at 0.
Example Tests
The first four multiples of 3 start at 3, not 0
Input: {"n":3,"count":4}
Expected: [3,6,9,12]
Multiples of 5 with a different count
Input: {"n":5,"count":3}
Expected: [5,10,15]
Asking for one multiple returns just n
Input: {"n":7,"count":1}
Expected: [7]
def multiples_of(n, count):
"""
Build the first count positive multiples of n.
Args:
n: the number to multiply
count: how many multiples to produce
Returns:
A list like [n, 2n, 3n, ...] with count items.
"""
# YOUR CODE HERE
passRun your code to see results
⌘↵ runs against the visible tests