Felix Rosén Felix Rosén - 3 months ago 10
Python Question

pyschool Topic 4 Q 1

The task is:


Create a function generateNumbers(num) that takes in a positive number as argument and returns a list of number from 0 to that number inclusive. Note: The function range(5) will return a list of number [0, 1, 2, 3, 4].


my code:

def generateNumber(num):
numbers = []
for x in range(num):
numbers.append(range(num+1))
return numbers


What is wrong? It returns a list that looks like
[[0,1,2,3,4]]
with double
"[]"
and for
generatenumber(0)
it returns
None
...

Answer

You are redundantly using range

You have to instead do

def generateNumber(num):
    numbers = []
    for x in range(num+1):
        numbers.append(x)
    return numbers         #Note where the return is

else the best you can do is

def generateNumber(num):
         return (list(range(num+1)))

Both will give you the desired result

Comments