-2

I want user to input the values into the string, using the user defined function i want it to return the even numbers.

I am trying to call filter_even() and pass evens as an argument.

def filter_even(evens):
    result_list = []
    for number in evens:
        if number % 2 == 0:
            result_list.append(number)
    return result_list

evens = list(input('Enter the numbers to be filtered'))
filter_even(evens)
Guy
  • 46,488
  • 10
  • 44
  • 88
greeshma y
  • 37
  • 9
  • 2
    Do you have a question? – Guy May 18 '23 at 12:21
  • 1
    I think `input` gives you a string and you're converting that to a `list` when you should probably `.split()` the string by spaces and then convert to `int` values. But you're also probably getting a `TypeError`, and haven't stated what's going wrong, so you're getting downvoted. – Tim Tisdall May 18 '23 at 12:26
  • 1
    Does this answer your question? [Get a list of numbers as input from the user](https://stackoverflow.com/questions/4663306/get-a-list-of-numbers-as-input-from-the-user) – JonSG May 18 '23 at 13:00

1 Answers1

0
  • You should use *evens as the parameter to pass whatever the user enters into a tuple
  • You should use evens = int(input('Enter the numbers to be filtered')) to receive user input as an integer

Your code should look like this:

def filter_even(*evens):
    result_list = []
    for number in evens:
        if number % 2 == 0:
            result_list.append(number)
    return result_list
evens = int(input('Enter the numbers to be filtered'))
tdy
  • 36,675
  • 19
  • 86
  • 83
  • I think instead of changing the way data is inputted into the function, splitting the input based on spaces would be more appropriate. As the current system will require multiple inputs from the user instead of one input with all values separated by spaces. – Harsh May 18 '23 at 22:18