In the following program I have a few questions.

results.txt
Johnny 8.65
Juan 9.12
Joseph 8.45
Stacey 7.81
Aideen 8.05
Zack 7.21
Aaron 8.31

scores = {}

result_f = open("results.txt")

for line in result_f:
    (name, score) = line.split()
    scores[score] = name

result_f.close()

print("The toop scores were:")

for each_score in sorted(scores.keys(), reverse = True):
    print("Surfer " + scores[each_score] + " scored " + each_score)

(name, score) = line.split()
scores[score] = name

Why is the value "name" being placed into the variable [score]? It looks to me like it's just swapping places with name and score.

Secondly, what is reverse = Ture? Is reverse a function, because it doesn't look like one. It looks to me like a simple statement inside of the sorted function and after the keys function.

Last but not least, when I run my program, it doesn't display the score Juan 9.12.

Thanks for any and all response.

The code is nice style, but it has one problem of generality: it uses string comparison for numbers from line split which are not necessary in same length of numbers.

reverse= True, gives value True to parameter reverse. By giving name of parameter you can not mix up the parameters in wrong order.

This codes output: (added one line to results)

>>> 
Name: Johnny, Score: 8.65
Name: Juan, Score: 9.12
Name: Joseph, Score: 8.45
Name: Stacey, Score: 7.81
Name: Aideen, Score: 8.05
Name: Zack, Score: 7.21
Name: Aaron, Score: 8.31
Name: Tony, Score: 10.2
----------------------------------------
The top scores were:
Surfer Juan scored 9.12
Surfer Johnny scored 8.65
Surfer Joseph scored 8.45
Surfer Aaron scored 8.31
Surfer Aideen scored 8.05
Surfer Stacey scored 7.81
Surfer Zack scored 7.21
Surfer Tony scored 10.2
****************************************
Name: Johnny, Score: 8.65
Name: Juan, Score: 9.12
Name: Joseph, Score: 8.45
Name: Stacey, Score: 7.81
Name: Aideen, Score: 8.05
Name: Zack, Score: 7.21
Name: Aaron, Score: 8.31
Name: Tony, Score: 10.2
The top scores were:
----------------------------------------
Surfer Tony scored  10.2
Surfer Juan scored  9.12
Surfer Johnny scored  8.65
Surfer Joseph scored  8.45
Surfer Aaron scored  8.31
Surfer Aideen scored  8.05
Surfer Stacey scored  7.81
Surfer Zack scored  7.21

The code:

from __future__ import print_function
scores = {}

result_f = open("results.txt")

## added line Tony 10.2 for results
for line in result_f:
    (name, score) = line.split()
    print('Name: %s, Score: %s' % (name,score))
    scores[score] = name

result_f.close()

print('-'*40)

print("The top scores were:")
## lexicographic sort can not do right sort for numbers with different number of numbers
for each_score in sorted(scores.keys(), reverse = True):
    print("Surfer " + scores[each_score] + " scored " + each_score)


## correction
## delete wrong dictionary
del scores
scores = dict()

print('*'*40)

for line in open("results.txt"): ## for with automatically closing file
    name, score = line.split()
    print('Name: %s, Score: %s' % (name,score))
    scores[float(score)] = name ### number, not string

print("The top scores were:")
print('-'*40)


for each_score in sorted(scores.keys(), reverse = True):
    ## multiple arguments, automatic space between, any type printing possible
    print("Surfer",scores[each_score],"scored ",each_score)

(name, score) = line.split()
scores[score] = name

Why is the value "name" being placed into the variable [score]? It looks to me like it's just swapping places with name and score.

Here scores is the name of the dictionary, score is the key and name is the value
mydict[key] = value
is one way to build up a dictionary

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.