Python program that repeatadly counts odd and even digits and sums them up
Given a number, how can we count the num开发者_如何学JAVAber of odd and even digits and then sum them up in Python? Apparently if you do this repeat this process, you will always end up with 123 for any number. I would like to do this in Python.
Example:
- 5683474
- 4 3 = 7 (4 even and 3 odd) 4, 3 and 7 now make up a new number.
- 1 2 = 3
We've ended up with 123 and this process works with every number.
Since this is probably homework, here are a few hints:
- You can convert an integer into a string using the
str()
function - You can convert a string into a list of characters using the
list()
function. - You can convert a character to an integer (assuming it's a number) using the
int()
function - An even number has a remainder of 0 when divided by 2
s = raw_input('Enter a number')
while s!= '123':
s = "%s"*3%(sum(~int(i)&1 for i in s),sum(int(i)&1 for i in s),len(s))
print s
精彩评论