简体   繁体   English

用户已输入PYTHON 3的偶数计数

[英]Count Even Numbers User has Inputted PYTHON 3

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. 一个函数可以通过返回t / f来判断一个数字是奇数还是偶数,另一个函数将调用第一个函数,然后返回有多少个偶数。

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. 这将保持输出0,并且实际上不计数。 What am I doing wrong now? 我现在在做什么错?

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. 您需要将字符串split()分成文本数字序列,然后将其转换为整数。


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. 有一种通用方法可以使用模数/余数运算符%来确定数字是奇数还是偶数:如果除以2后的余数为0则数字为偶数。

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. 您正在计算整数-[0,2,4,6,8]等-是否为字符串中的字符-“ 0”,“ 2”,“ 4”,“ 6”,“ 8”等。目前, IsEvenDigit(a)永远不会为真,因为字符串中的字符不会出现在偶数整数列表中,因此if语句下面的代码将永远不会执行。 You need IsEvenDigit(int(a)) in the CountEven function. 您需要在CountEven函数中使用IsEvenDigit(int(a))

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. 在另一个主题上,您的帖子的评论者建议阅读PEP8。您的代码实际上格式化得很好,仅在Python中使用,CamelCase仅用于类,而words_seperated_by_underscores用于变量和函数名。

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! 它确实定义了2个(匿名)函数!

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. 这也将处理所有其他数字。

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. 因此,通过在CountEven函数范围之外调用CountEvent(d),就不必使用递归,而是在定义函数之后简单地调用它。

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

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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM