Count Even Numbers User has Inputted PYTHON 3

2020-04-19 05:05发布

问题:

I must create two functions. One that can tell whether one number is odd or even by returning t/f, and the other will call the first function then return how many even numbers there are.

This is my code so far:

    Even = [0,2,4,6,8]
    IsEvenInput = int(input("Please enter a number: "))

    def IsEvenDigit(a):
        if a in Even:
            return True
        else:
            return False

    y = IsEvenDigit(IsEvenInput)
    print(y)


    def CountEven(b):
        count = 0
        for a in b:
            if IsEvenDigit(a):
                count+=1
        return count
    d = input("Please enter more than one number: ")
    y = CountEven(d)
    print(y)

This keeps outputting 0 and doesn't actually count. What am I doing wrong now?

回答1:

Here is another approach:

def is_even(number):
    return number % 2 == 0

def even_count(numbers_list):
    count = 0

    for number in numbers_list:
        if is_even(number): count += 1

    return count

raw_numbers = input("Please enter more than one number: ")
numbers_list = [int(i) for i in raw_numbers.split()]

count = even_count(numbers_list)
print(count)

This will take care of all other numbers too.



回答2:

d = input("Please enter more than one number: ")

This is going to return a string of numbers, perhaps separated by spaces. You'll need to split() the string into the sequence of text digits and then turn those into integers.


There's a general approach to determining whether a number is odd or even using the modulus / remainder operator, %: if the remainder after division by 2 is 0 then the number is even.



回答3:

So by calling CountEvent(d) outside the scope of the function CountEven, you aren't using recursion, you're simple calling the function after it's been defined.

Try reducing the amount of code outside of your functions.

    #Start by declaring your functions:
    def isEven(n):
        return n % 2 == 0

    def countEven():
        count = 0
        string_of_numbers = input("Please enter numbers separated by spaces: ")
        list_of_number_characters = string_of_numbers.split(' ')
        for number in list_of_number_characters:
            number_as_int = int(number)
            if isEven(number_as_int) == True:
                count = count + 1
        print("There were " + str(count) + " even numbers found.")


    countEven() #Call the function that runs your program


回答4:

You are counting whether the integers - [0, 2, 4, 6, 8] etc. - are characters in a string - "0", "2", "4", "6", "8" etc. Currently, IsEvenDigit(a) will never be true, because a character in a string will not be in the list of even integers, so the code beneath the if statement will never be executed. You need IsEvenDigit(int(a)) in the CountEven function.

On another topic, a commenter to your post suggested reading PEP 8. Your code is actually formatted pretty well, its just in Python, CamelCase is used just for classes, and words_seperated_by_underscores is used for variables and function names.

Or if you want brevity and unreadability, some code:

main = lambda: sum(map(lambda x: int(x) % 2 == 0, (i for i in input("Enter a number: "))))
main()

It does define 2 (anonymous) functions!



回答5:

A possible solution:

def is_even(n):
    return not n % 2

def count_even(numbers)
    return sum(map(is_even, numbers))

nums = input("Enter numbers separated by spaces: ")
nums = map(int, nums.split())
print(count_even(nums))


标签: python count