Define a function count_down() that consumes a number a produces a string counting down to 1
Question:
Define a function count_down() that consumes a number a produces a string counting down to 1.
count_down(3) => "3, 2, 1"
count_down(0) => ""
count_down(1) => "1"
Answers:
A possible solution could look like
def count_down(n):
return ', '.join(str(i) for i in range(n, 0, -1))
# return ', '.join(map(str, range(n, 0, -1)))
>>> count_down(5)
'5, 4, 3, 2, 1'
join
, str
, range
are very fundamental functions/methods you should read up on in the documentation.
Define a function count_down() that consumes a number a produces a string counting down to 1.
count_down(3) => "3, 2, 1"
count_down(0) => ""
count_down(1) => "1"
A possible solution could look like
def count_down(n):
return ', '.join(str(i) for i in range(n, 0, -1))
# return ', '.join(map(str, range(n, 0, -1)))
>>> count_down(5)
'5, 4, 3, 2, 1'
join
, str
, range
are very fundamental functions/methods you should read up on in the documentation.