Count Even Numbers User has Inputted PYTHON 3

前端 未结 5 1226
死守一世寂寞
死守一世寂寞 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: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!

提交回复
热议问题