Problems in the execution of a function

Asked

Viewed 28 times

-1

I was trying to create a Python function that would display only even numbers within a range of numbers, until it works and displays numbers, but it also displays 'None' where, I imagine, the odd numbers are. If anyone knows how to fix this and help me I’ll be grateful.

Essa é a função e o resultado

  • Guy next did puts the code. It gets really bad by picture

  • Beauty, it’s the first time using the site kkk

  • No bullshit, man. If you can, put your code in. Just to be right.

1 answer

0

It turns out that its function numpar has no return when the number is not even, as it does not have an explicit return, it will end up returning None.

A possible solution is to make your function in a pair not return a number but a logical value, indicating whether the number received is even or not:

def numpar(numero):
  return numero % 2 == 0

for numero in range(0,20):
  if numpar(numero):
    print(numero)
  • Thank you, Daniel! It worked right, now I understand what the problem was.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.