Count Even Numbers User has Inputted PYTHON 3

前端 未结 5 1184
死守一世寂寞
死守一世寂寞 2021-01-24 03:11

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 t

相关标签:
5条回答
  • 2021-01-24 03:39

    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.

    0 讨论(0)
  • 2021-01-24 03:46
    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.

    0 讨论(0)
  • 2021-01-24 03:49

    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))
    
    0 讨论(0)
  • 2021-01-24 03:51

    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
    
    0 讨论(0)
  • 2021-01-24 03:51

    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!

    0 讨论(0)
提交回复
热议问题