-4

why doesn't it return anything to the console? Could it have been due to the programme i'm using to run it? (Spyder 5.1.5) and if so how do i get the function to run and also show up in console.

def check_even_list(number_list):    
    for number in number_list:
        if number % 2 == 0:
            return True
        else:
            pass 

check_even_list([1,2,4,5,7,4,2,5,6,2])
Ryan M
  • 18,333
  • 31
  • 67
  • 74
Lightupp
  • 3
  • 2
  • 4
    Aside: `else: pass` is completely redundant. – Konrad Rudolph Apr 08 '22 at 11:22
  • 4
    Because you don't tell it to `print` to the console – BrainFl Apr 08 '22 at 11:23
  • if you want to print something, use the `print()` function – godo57 Apr 08 '22 at 11:24
  • Well, for me it returns/displays `True` and that is okay, because you have an even number (2) in your list. And if this even number is found the function will return. So where is the problem? Please [edit] your question and describe your problem with more detail! – Ocaso Protal Apr 08 '22 at 11:24
  • is the question better now? Im new to all of this, sorry! – Lightupp Apr 08 '22 at 11:35
  • I don't know Spyder, but it looks like all the oher comments were correct then: You need to add a print somewhere e.g. `print(check_even_list([1,2,4,5,7,4,2,5,6,2]))` – Ocaso Protal Apr 08 '22 at 11:48
  • The result will only "show up" if you print it – DarkKnight Apr 08 '22 at 11:49
  • tools like `Spyder`, `Juputer` or even `Python in interactive mode` tries to make life easier and they automatically display result from last command - but in real script/program you have to use `print()` to display it. This way you can decide what you want to see on screen. – furas Apr 08 '22 at 12:14

1 Answers1

0

It looks like you need to know if there are any even numbers in the list. You should arrange for your function to return a boolean (True/False) appropriately.

For example:

def check_even_list(number_list):
  return any(map(lambda x: x % 2 == 0, number_list))

print(check_even_list([1,2,4,5,7,4,2,5,6,2]))

Output:

True
DarkKnight
  • 19,739
  • 3
  • 6
  • 22