本文介绍了用户已输入PYTHON 3的偶数计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须创建两个函数。一个可以通过返回t / f来判断一个数字是奇数还是偶数,另一个函数将调用第一个函数,然后返回有多少个偶数。

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)

这将保持输出0并不会实际上不算在内。我现在在做什么错?

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

推荐答案

这是另一种方法:

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.

这篇关于用户已输入PYTHON 3的偶数计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 12:23