[Tutor] A simple solution to the even/odd problem
Jesse
intermezzoooh at gmail.com
Fri Apr 21 04:40:20 CEST 2006
Hey, I'm a Python newbie, and I'm not even sure I've correctly interpreted
the problem, but from what I gather the idea is to take an integer with an
arbitrary number of digits and return two [strings/lists/tuples/whatever]:
one containing all of the odd digits, and another containing all of the even
digits. This should work:
def make_string(words):
string = " "
for x in words:
string = string + x + " "
return string
def even_odd(num):
even = []
odd = []
num_string = str(num)
for x in num_string:
a = int(x)
if a%2 == 0:
even.append(x)
else:
odd.append(x)
odd_string = make_string(odd)
even_string = make_string(even)
return (odd_string, even_string)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060420/adc6b1c3/attachment.html
More information about the Tutor
mailing list