Making a code that makes even numbers 1 and odd numbers 2 in a multiple digit number

First of all, this is not related to this course. I was trying to find somewhere to post this question but I couldn’t find an area that wasn’t course related, so if this is not allowed just tell me.

Moving on:
I want to create a simple code that takes a number (ex: 345122) and makes even numbers 1 and odd numbers 2, and then prints the number (212211).

This is what I was thinking:

number = input('Write a number ')

for i in range(len(number)):
    if int(number[i])%2 == 0:
        number[i] = 1
    else:
        number[i] = 2

print(number)

The problem in this is that you can’t change an index’s content in a string. How else can I do this? Should I make it a list and then add them all together? How do I do that?

That would work. You won’t be adding, though, but joining.

result = []
for num in number:
    if num % 2:
        result.append(2)
    else:
        result.append(1)
print ''.join(result)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.