0

I have a problem while trying to use the zip function to sync two lists together. I'm creating a high score system with txt file. It reads the file and splits up the file into two lists, the names and scores eg:

5   
andy  
34  
jamie  
8   
fred  
45  
kyle  
6  
joe  

Then it sorts one list and the other sync it. This works fine. It then asks for a new score to be entered, and if it's bigger than the smallest value in the list, the smallest one is removed and the new value is added to the list. I then try to sort it again, but it fails and gives me this error message:

list1, list2 = (list(t) for t in zip(*sorted(zip(list1, list2))))
TypeError: '<' not supported between instances of 'str' and 'int'

I've tried to switch the list variables to strings but it just outputs a load of rubbish:

[' ', ' ', ' ', ' ', "'", "'", ',', ',', ',', ',', '3', '4', '4', '5', '6', '7', '8', '[', ']']
[',', ',', 'o', 'r', ' ', 'j', "'", "'", 'f', 'j', ' ', "'", 'e', 'd', "'", "'", 'e', '[', 'a']

Below I've put my code and a link to where I got the 'sync list' code from.

Thanks

lines = [line.rstrip('\n') for line in open('highScores_Test.txt')]
print(lines)

#list1 = scores list2 = names

list2 = [(lines[1]),(lines[3]),(lines[5]),(lines[7]),(lines[9])]
list1 = [int(lines[0]),int(lines[2]),int(lines[4]),int(lines[6]),int(lines[8])]

print(list1[0])



list1, list2 = (list(t) for t in zip(*sorted(zip(list1, list2))))


print(list1)
print(list2)

num = input()
name = input()

if int(num) > int(list1[0]):
    del list1[0]
    del list2[0]
    list1.append(num)
    list2.append(name)

list1, list2 = (list(t) for t in zip(*sorted(zip(list1, list2))))

print(list1)
print(list2)

Sync list thread

roschach
  • 8,390
  • 14
  • 74
  • 124
Joshlucpoll
  • 189
  • 3
  • 12

1 Answers1

0

num is str, and list1 is list of int.

instead of

list1.append(num)

you should use

list1.append(int(num))
He Xiao
  • 71
  • 9