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!

For 6 and 7, you should look into the module itemgetter() (Do a ctrl+f for getter on this page: http://docs.python.org/library/operator.html)

If you have simple items in a list, for example a list of ints:

mylist=[1,2,5,3,7,8]

Then you can use simply mylist.sort() to sort them. If you have strings, it will sort them alphabetically. But if you have more complex data types like in your case, a person and a score, you can store them in a list of tuples aka:

mylist=[(Bill, 93), (Adam, 100), (Josh,32)]

(Giving myself 100% of course)... then you can use the itemgetter to sort these by the second value in the tuple. This is a handy import because it can be used for even more complex tuples, like a tuple of 3 names and 15 scores.

Once you have the new sorted object, just use obj.reverse() to output it in the opposite order (ascending or descending).

Now remember that the key of a dictionary has to be unique. Names with the same score have to processed so the names appear in a list.

Here is an example how to handle key collisions:

# has 2 names with matching scores
data_str = """\
Mark 50
Adam 20
Tony 70
Frank 50"""

fname = "score_file.txt"
# write the data file
with open(fname, "w") as fout:
    fout.write(data_str)

# read the data file back in
# and convert to a score:name dictionary
score_dict = {}
for line in open(fname):
    #line = line.strip()
    name, score = line.split()
    # this will not handle matching scores
    #score_dict[int(score)] = name
    # this handles score collisions
    score_dict.setdefault(score, []).append(name)

print(score_dict)

'''
{'20': ['Adam'], '50': ['Mark', 'Frank'], '70': ['Tony']}
'''
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.