6. What are the names of the winners?

6.1. Using another list

  • We can use two lists to find out.

  • The names and scores lists to save the names and scores obtained by the participants respectively.

Data file: surf4_en.txt

Activity: 6.1.2 ActiveCode (ac_l37_6a_en)

6.2. But these data are incorrect!

  • There must be a problem because Zack is really bad!

  • What happened?

  • By sorting the names list in decreasing order, the character 'Z' ends up being the first.

  • The correspondence between scores and the names of the participants is lost.

  • Another data structure is needed to avoid losing the correspondence.

6.3. We need to join the lists

6.4. Using and sorting a dictionary

  • Using a dictionary and seeing all its elements iteratively.

1scores = {}
2scores[9.12] = "Juan"
3scores[7.21] = "Zack"
4
5for participant in sorted(scores, reverse=True):
6    print(f"{scores[participant]} {participant:.2f}")
line that just executed

next line to execute

Print output (drag lower right corner to resize)
Frames
Objects

Activity: CodeLens 6.4.1 (cl_l37_6_en)

  • Using a dictionary for the championship.

Activity: 6.4.2 ActiveCode (ac_l37_6b_en)

On a scale from 1 (needs improvement) to 3 (excellent), how would you rate this chapter?




Activity: 6.4.3 Poll (TWP37E)

You have attempted 1 of 5 activities on this page