Hi all I need help with the final part of my practice problems for class, if anyone who can show me what to do it would be greatly appreciated!!
Write a program that incorporates the following functionality:
1) Prompt the user for a series of names and scores.
2) Store each name and score pair in a dictionary (indexed by score).
3) Write the names and scores to a text file - one pair per line.
4) Read the file line by line and insert each name/score pair into a new dictionary.
5) Display the results. For example:
Mark 74
Jill 83
Jack 44
etc.
6) Sort the results by score.
7) Display the sorted results in descending (or non-ascending) order. For example:
Jill 83
Mark 74
Jack 44
etc.
Here's My Code as of now:
def getScore(scores):
name = input('Please enter a name: ')
score = int(input('Please enter ' + name + '\'s score: '))
scores[score] = name
def getScores(scores):
keepgoing = True
while(keepgoing):
getScore(myscores)
reply = input('Do you want to enter another score? ')
if(reply == 'y'):
continue
else:
keepgoing = False
def saveScores(scores, filename):
out_file = open(filename, "wt")
for num in scores.keys():
out_file.write(str(num) + " " + scores[num] + "\n")
out_file.close()
def readScores(scores, filename):
file = open(filename)
name = ''
score = ''
for line in file:
score, name = line.split()
scores[int(score)] = name
infile = open("scores.txt","r")
for line in infile:
values = line.split()
scores[values[0]] = values[1]
print('Name: ', values[1], 'score', values[0])
infile.close()
for key in sorted(scores.keys(), reverse=True):
print(scores[key], key)
def printScores(scores):
for num in scores.keys():
print(scores[num] + " " + str(num))
def printSorted(scores):
for num in sorted(scores.keys(), reverse=True):
print(scores[num] + " " + str(num))
myscores = {}
getScores(myscores)
saveScores(myscores, 'scores.txt')
newscores = {}
readScores(newscores, 'scores.txt')
print(newscores)
print('==============================')
print(sorted(newscores.items(), reverse=True))
print('==============================')
print('==============================')
printScores(newscores)
print('==============================')
printSorted(newscores)
print('==============================')
This is my text file(scores.txt)
84 frank
76 mary
52 john
I am looking for help with numbers 6 and 7 as my professor really hasn't taught us how to do these steps!
Thank You!