hw2 couldnt debug the line:

G
Gülümser Aslı Kütük (310 points)
2 4 6
asked Nov 6, 2021 in HW2 by Gülümser Aslı Kütük (310 points)
recategorized Nov 7, 2021 by andrea.sterbini

hello guys! I couldnt debug the line:

 for letter in player1list:
            
            if abs(ord(player1list[letter])-ord(player2list[letter]))<=k:


I splitted the string letters into lists like player1list. However when I try to compare the unicodes of the letters in the lists, I get an error. the terminal says "list indices must be integers or slices, not str"

What should I do?

111 views

3 Answers

Best answer
gianluca5539 (9820 points)
3 6 44
answered Nov 7, 2021 by gianluca5539 (9,820 points)
selected Nov 7, 2021 by Gülümser Aslı Kütük
Letter in your case becomes the single value inside the list, not its index. You cannot use it to get values from another array (player2list)

What you can do is do a for loop that lets you use an incrementing index so that you can get values from both lists with it. Try using the range function, you can look it up on python docs. Hope you find this helpful, bye!
G
Gülümser Aslı Kütük (310 points)
2 4 6
commented Nov 7, 2021 by Gülümser Aslı Kütük (310 points)
Yes very helpful. Thank you!
andrea.sterbini (172680 points)
511 927 1776
answered Nov 6, 2021 by andrea.sterbini (172,680 points)
Letter is the index in the string?
G
Gülümser Aslı Kütük (310 points)
2 4 6
commented Nov 7, 2021 by Gülümser Aslı Kütük (310 points)
yes it is like ith element of the string.
gabriel.bustamante (2540 points)
3 5 20
answered Nov 6, 2021 by gabriel.bustamante (2,540 points)
Is player1list similar to ["a", "b", "c"]? In this case "letter" would be a string (like "a"), which can't be used as the index of a list. If you'd like to access the player-lists through indices "letter" should be an integer and you should iterate through a list of integers
G
Gülümser Aslı Kütük (310 points)
2 4 6
commented Nov 7, 2021 by Gülümser Aslı Kütük (310 points)
I see thank you!