Please ignore secret bonuses. Secret tests do NOT award bonus. Max hw grade is 30+2 bonus efficiency

Do you need help?

hw2 couldnt debug the line:

G
Gülümser Aslı Kütük (310 points)
2 4 6
in HW2 by (310 points)
recategorized by

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?

234 views
closed

3 Answers

Best answer
gianluca5539 (9820 points)
4 6 44
by (9.8k points)
selected by
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
by (310 points)
Yes very helpful. Thank you!
andrea.sterbini (207920 points)
750 1267 2373
by (208k points)
Letter is the index in the string?
G
Gülümser Aslı Kütük (310 points)
2 4 6
by (310 points)
yes it is like ith element of the string.
gabriel.bustamante (2540 points)
3 6 20
by (2.5k 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
by (310 points)
I see thank you!