[Python] code unable to distinguish between duplicate values in a list

Discussão em 'Python' iniciado por Stack, Março 5, 2025.

  1. Stack

    Stack Membro Participativo

    I am a beginner learning python, trying to accomplish the following:

    Given two strings of equal length, e.g:

    string_1 = 'Hello' string_2 = 'World' print them vertically side by side, with a space between each character:

    H W
    e o
    l r
    l l
    o d


    however I am encountering an issue with my code where the first and second 'l' in 'Hello' are being treated as the same letter, which is leading to some issues in output

    here is my code:

    string1 = 'Hello'
    string2 = 'World'
    indx = ''

    for letter in string1:
    indx = int(string1.index(letter))
    print(letter + ' ' + string2[indx])


    it ends up giving the result as shown(area of concern bolded for easier viewing):

    H W
    e o
    l **r**
    l **r**
    o d


    instead of:

    H W
    e o
    l **r**
    l **l**
    o d


    My question is how I can modify the code such that it can distinguish between the 2 'l's in 'Hello', or if my code was entirely wrong to begin with. Am not sure how to proceed with this

    As I am just starting out I am not sure how to deal with this issue and hope to get some advice on how to rectify the issue and improve the code.Thanks.

    Continue reading...

Compartilhe esta Página