Code to output the first repeated character in given string?

后端 未结 9 2137
日久生厌
日久生厌 2021-01-21 15:23

I\'m trying to find the first repeated character in my string and output that character using python. When checking my code, I can see I\'m not index the last character of my co

9条回答
  •  悲&欢浪女
    2021-01-21 15:41

    There were a few issues with your code...

    1.Remove -1 from len(letters)

    2.Move back one indent and do b = b + 1 even if you don't go into the if statement

    3.Indent and do a = a + 1 in the first for loop.

    See below of how to fix your code...

        letters = 'acbdc'
        for a in range(0, len(letters)):
            # print(letters[a])
            for b in range(0, len(letters)):
                # print(letters[b])
                if (letters[a] == letters[b]) and (a != b):
                    print(b)
                b = b + 1
            a = a + 1
    

提交回复
热议问题