Jump to content


Chapter 5 Surf Program

  • Please log in to reply
1 reply to this topic

#1 jgeter


    New Member

  • Members
  • Pip
  • 4 posts

Posted 29 June 2015 - 02:03 PM

scores = {}       
result_f = open("results.txt")
for line in result_f:
    (name, score) = line.split()
    scores[score] = name
print ("The top scores were:")
for each_score in scores.keys():
    print ('Surfer ' + scores[each_score] + ' scored ' + each_score)
the line scores[score]=name is actually getting the surfer name from the file or is it  associating the name with the score putting the line back together so to speak after it was split?

#2 #TM#


    Active Member

  • Members
  • PipPipPip
  • 91 posts
  • Gender:Not Telling

Posted 02 July 2015 - 10:41 AM

Hi jgeter, It is doing neither.


It is actually populating the dictionary.

scores = {}

means dictionary is initialized. But you have to read the file "results.txt" and populate the dictionary with scores and names. So,

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

In here you are reading each line of the file, then splitting the name and score and storing the score (as key) and respective name (as value) in the dictionary.


For more follow the below link:





Edited by #TM#, 02 July 2015 - 10:55 AM.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users