Felix Rosén - 2 months ago 5x

Python Question

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]]`

`"[]"`

`generatenumber(0)`

`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

Source (Stackoverflow)

Comments