[open] Sorting randomly generated values with names
Hi! I'm trying to display on the sketchpad (in descending order) fake players with randomly generated scores as well as the participant's actual player name and score. Below is what I have so far.
However, I keep receiving an error message stating that "an integer is required" at this line:
sorted_players = sorted(player_name, key=itemgetter(1), reverse=True)
Any ideas why this code isn't working? I've tried a few different versions of this code, and I get this error every time!
import random
#randomly generate scores for each player
jsw_score = random.randint(70,90)
jbp_score = random.randint(70,90)
bsp_score = random.randint(70,90)
mjk_score = random.randint(70,90)
phs_score = random.randint(70,90)
msw_score = random.randint(70,90)
tdl_score = random.randint(70,90)
aik_score = random.randint(70,90)
wjc_score = random.randint(70,90)
#add player names to player scores
from operator import itemgetter, attrgetter, methodcaller
player_name = [
('JSW', jsw_score),
('JBP', jbp_score),
('BSP', bsp_score),
('MJK', mjk_score),
('PHS', phs_score),
('MSW', msw_score),
('TDL', tdl_score),
('AIK', aik_score),
('WJC', wjc_score),
('sub_name', sub_score)
]
#sort in descending order
sorted_players = sorted(player_name, key=itemgetter(1), reverse=True)
#access highest score ---> lowest score in order
[sorted_players[0]]
[sorted_players[1]]
[sorted_players[2]]
[sorted_players[3]]
[sorted_players[4]]
[sorted_players[5]]
[sorted_players[6]]
[sorted_players[7]]
[sorted_players[8]]
[sorted_players[9]]
Comments
Hi,
Are you sure the variable "sub_score" is an integer as well?
Cheers,
Josh