+ 1
How can I make a function that receives an integer and counts the number of odd and even digits?
Must be using the lambda function.
4 ответов
+ 3
I think you should define the range of the numbers, then use the range to create two diff list, one for even digits, and another for odd digits, then print the length of each list...
Happy coding 😊 Keep coding 🙏
+ 2
If you need to use lambda, you are probably meant to use a higher order function, like filter()
You can convert a number to a list of digits first:
digits = list(map(int, str(number)))
Then use filter to take only odd ir even digits, and count them with the len() function.
odds = len(list(filter(lambda d: d%2==1, digits)))
Alternatively you could achieve the same by using sum() and map(), converting each digit to True or False, based on them being odd or even.
+ 1
def count(num):
o=0
e=0
print(" number : ",num)
for i in num:
if int(i)%2==0 :
e+=1
else:
o+=1
print(" even : ",e)
print(" odd : ",o)
n=input()
count(n)