Sorting a txt file with python alphabetically?

Top of my code

player_name = input('Please enter your first and last name: ')

player_class = input('Please enter your class. {Class 1, Class 2 or Class 3}: ')

player_class = "N:General homeworkComputer ScienceClass Files" + 
player_class + '.txt'

Bottom of my code

average_score = (int(score_1) + int(score_2) + int(score_3)) / 3

write_to_file = player_name + ' ' + str(score_1) + "/10" + ' ' + str(score_2) + "/10" + ' ' + str(score_3) + "/10" + ' Average is ' + str(round(average_score))
 + "/10" + "n"

file = open(player_class,"a")

file.write(write_to_file)

file.close()

I’ve been searching for a while, and I have no idea how to alphabetically sort this. If i were to input the name as Ben and class as Class 1, then as Alice and Class 1, it would still show Ben first. Ive tried the sorted{} function and it just doesnt work. I need this for an assessment and have no idea what to do! Please help if you can 🙁


Source: python

Leave a Reply