Sort By Score Challenges
After completing the code-along, attempt the challenges below.
Optional Practice: Bug Fixes
View these buggy Repl projects and try to fix them. To make things easier, reference the completed code from the code-along, and compare it to the buggy code. Test each program and make sure that they all work properly!
1. Add More Data
Now, getting back to the code-along code. The current list only has a few items, so sorting them isn't all that impressive. Add at least three more dictionary items to the players
list.
- Add a comma at the end of the last item in the list
- Create the structure for the new dictionary:
{}
- Within the new dictionary, add a
"name"
key with a new value - Within the new dictionary, add a
"score"
key with a new value - Repeat until there are at least three new items
- Run the code, and verify that the sorting still works based on the score!
2. Multiple Scores
Often, a score will be an amalgamation of several scores. For example, the total score of a football game is the sum of the scores from each quarter. Track multiple scores for each dictionary item in the list.
- Add a
"score2"
key to each item in the list - Give a value to each
"score2"
property - In the other part of the code, find the
find_min_idx
function definition - Find the
if
statement where two things are compared - Update the condition so that it takes both
"score"
and"score2"
into account- It should check the
score
plus thescore2
value
- It should check the
- Find the part of the code that prints out the top players
- Update it so it prints the total score instead of simply the score
- Again, it should be
score
plusscore2
- Again, it should be
- Run the program, and verify that the sorting works with the new scores!
3. Custom Players
This challenge may be fairly challenging. Feel free to skip down to the next one if desired.
Right now, the program only uses hard-coded players. This works for demonstrative purposes, but it would be more useful if the user of the program could add their own data. Update the code so this is possible!
- Place the sorting code in a new function:
sort
- Take in the collection, and run the existing
while
code on it
- Take in the collection, and run the existing
- At the bottom of the file, create a
while True
loop that repeats infinitely - In the body of the loop, ask the user what to do
- Create an
if
for each command - If they want to add a player:
- Ask them for player information
- Create a new player dictionary
- Add the new dictionary to the
players
list
- If they want to see the players:
- Sort the players list
- Print out the players in the list
- If they want to exit:
- Use
break
to exit the loop
- Use
This should make it possible for users to add as many players as they would like, and see them sorted!
BONUS: Multiple Sorting Methods
Often, when viewing data, it is possible to sort the data in multiple ways. Add in the ability for the program to sort the players by:
- Score (low to high)
- Name (alphabetical)
- Name (reverse alphabetical)
4. Rock Paper Scissors Redux
This challenge could be somewhat challenging.
This challenge is totally separate from the score sorting program; start by making a new Repl project and go from there.
The goal is to create a Rock, Paper, Scissors game using a dictionary. It should be similar to the Rock Paper Scissors code, but it will simplify things a bit.
A Dictionary of Victories
The old way of doing Rock Paper Scissors involved several if
statements. Those worked, but there seemed to be a lot of repetitive code. By using a dictionary, the code will be much more dynamic.
Here's the idea: create one dictionary object where the keys represent a possible move, and the value represents the move that that move beats. Like this:
1. key: Rock, value: Scissors (rock beats scissors)
2. key: Paper, value: Rock (paper beats rock)
3. key: Scissors, value: Paper (scissors beats paper)
That's all the information needed to determine the winner of a match!
- At the top of the file, create a new variable named
victories
- Set the
victories
variable equal to a new dictionary:{}
- Add a key of
"R"
to the dictionary with a value of"S"
- Add keys of
"P"
and"S"
with the proper values
Now the code contains the dictionary determining which move beats which.
A Battle Function
The next step is to use the victories
dictionary to determine the winner between two competitors. This should happen in a function named battle
.
- Under the
victories
dictionary, define a new function namedbattle
def
keyword, space- function name (
battle
) - parentheses
- Parameter 1:
player
- Parameter 2:
computer
- In the indented body of the
battle
function, create anif
statement - For the
if
condition, check ifplayer
andcomputer
are equal - If they are, print out
"Tie"
- Outside of the
if
statement, create anelif
statement - For the
elif
, check if the value for thevictories
dictionary with a key ofplayer
is equal tocomputer
- If it is, print out
"Player Wins"
- Outside of that
if
, create anelse
- In the else, print out
"Computer Wins"
Run the code to make sure it works - but it shouldn't do anything yet!
Testing
Under the definition of the battle
function, call the battle
function with a few test inputs. Make sure that every possible outcome can be achieved.
For example, it could be called in this manner:
battle("P", "S")
The code above should cause the program to print out Computer Wins
, because paper loses to scissors.
5. Another Sorting Algorithm
This challenge is quite challenging.
Try to implement a different sorting algorithm to sort the list of players
! There is a lot of code available online to help. The biggest challenge is adapting the code to work for the specific solution. Here are some ideas:
More Challenging
These two algorithms are a little more challenging to implement - they require using recursion.