1

I have this codes

def display(s1,s2):
    l1 = list(s1)
    l2 = list(s2)
    l3 = [None]*10
    for i in range(10):
        for j in range(10):
            if i==l2[j]:
                l3[j] = l1[i]
    return l3

print display('3941068257', '1234567890')

Example: 3 in position 0 of list1; 0 in position 9 of list2 => we will display 3 in position 9 in new list called l3 and so on ....

so program supposes to display like that 9410682573 but it still display None none ......

My compiler doesn't have debugger so I don't know how to find out. Anyone can help?

0

1 Answer 1

5

You are comparing string (one-char element of l2) with integer. It always fails.

It is because the below happens:

l2 = ['3','9','4','1','0','6','8','2','5','7']  # when you do "l2 = list(s2)"
for i in [0,1,2,3,4,5,6,7,8,9]:  # when you do "for i in raange(10)"

The types just don't match.

Instead do this:

def display(s1,s2):
    l1 = list(s1)
    l2 = list(s2)
    l3 = [None]*10
    for i in range(10):
        for j in range(10):
            if str(i) == l2[j]:  # <-- change is here
                l3[j] = l1[i]
    return l3

print display('3941068257', '1234567890')
Sign up to request clarification or add additional context in comments.

1 Comment

@LongBodie: To display a list as one string just join it like that: ''.join(l3).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.