What's the problem on this function by which I tried to print the even number at the range of 1~50

Viewed 63

I'm a beginner at python and I want to make a python file which contains a function which prints the list of even number at the range of 1~50. this is my code.

def test():
    for int in range(1,51):
        if int%2==0:
            return int

    print(list(int))

test()

I ran it through Pycharm but nothing has been printed on my console. Can anybody give me some help?

7 Answers

I would advice the following:

  • Do not call a variable int since it's a python data type
  • If you want to print a list of results, you need to add the data to a list (called results below)
  • return means that you are exiting the function you are running

def test():
    results = []
    for i in range(1,51):
        if i%2==0:
            results.append(i)

    print(results)

test()

->

[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50]

You don't need a function for this

print(list(range(2,51,2)))

Your main issue is the return statement prevents the code from reaching the print statement, and you're never printing the return result to see the single int value you did return

Also, you're overriding the int() function by calling a variable int

You need to create a list and append the items to it to get the list of output. With the current program you are just returning a the first even number.

Although the use of int is allowed yet it is not recommended to use keyword for object names.

Try this:

def test():
    l = []
    for int in range(1,51):
        if int%2==0:
            l.append(int)

    print(l)

test()

You cant use int as a loop control variable since it is a keyword

def test():
    for num in range(1,51):
        if num%2==0:
            print(num) 
test()

The return statement will exit out of the function when first even number is found. The code below will fix the issue.

def test():
    for num in range(1,51):
        if num%2==0:
            print(num)
           

In your code int is a function or class or data type.

def test():
for i in range(1,51):
      if i%2==0:
           print(i)

test()

Here I am printing i which is even number. If it wasn't inside the for loop than it will only print 2.

def test():
for i in range(1,51):
      if i%2==0:
           return i

print(test())

Here what's happening it will only print 2. Cause, it unable to return more than a value. So, output : 2.

That's why you have to run the first code.

Here's something which you can try also.

for i in range(1, 51):
    if i % 2 == 0:
        print(i)

It is without any class.

There are few errors

  • Don't use any inbuilt name function like int for variable a name.

  • Function execution is completed right after the statement return.

  • Indentation is important, they define block scope.

     def test():
          for int in range(1,51): # use something other variable name for int
              if int%2==0:
                  return int
      # the function exist after the retun statement 
          print(list(int)) #  this statement wont be executed
    

A possible answer

def test():
    return [x for x in range(2, 51,2)]
print(test())
Related