-2
def number(n):
    x=[]
    for i in range (1,n):
        if i%2==0:
            x=x+[i]
            i=i+1
            
    return(n)

In the above code, no error is showing while running, but no value returned

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
vnm
  • 5
  • 1
  • 2
    That just defines the function; you also need to call it – Jiří Baum Aug 05 '21 at 11:28
  • 2
    You also probably meant to return `x` rather than `n` – Jiří Baum Aug 05 '21 at 11:30
  • Note that the ``i=i+1`` is superfluous. ``i`` will be immediately reassigned to the next value from the ``range``. On a minor note, ``return`` is not a function – its "argument" does not need to be wrapped in parentheses. – MisterMiyagi Aug 05 '21 at 11:35
  • Does this answer your question? [What is the purpose of the return statement? How is it different from printing?](https://stackoverflow.com/questions/7129285/what-is-the-purpose-of-the-return-statement-how-is-it-different-from-printing) – Karl Knechtel Aug 17 '22 at 05:40

2 Answers2

2

As already mentioned in the comments section, there are a few things to note:

  1. A function needs to be called (in case it was not done), so in your case you have to call the function by number(10) for example.
  2. Probably you wanted to return x instead of n.
  3. i = i+1 is not needed since range() already returns an iterable, therefore it has to be removed
  4. the code in front of return does not need to be inside parantheses
ilpianoforte
  • 1,180
  • 1
  • 10
  • 28
0

you must change your code :

def number(n):
    x = []
    for i in range(1, n):
        if i % 2 == 0:
            x = x + [i]
            i = i + 1

    return (x) #---> you must return x not n!

print(number(12))

output:

[2, 4, 6, 8, 10]

difference between return and print in python : this

Salio
  • 1,058
  • 10
  • 21