Homework 8: Python Practice with Detective Pikachu
Due: Tuesday November 12, 2019 at 9:00PM EST.

Handin
- You may submit as many times as you want. Only your latest submission will be graded. This means that if you submit after the deadline, you will be using a late day – so do NOT submit after the deadline unless you plan on using late days.
- Do not put your name anywhere in any of the handin files.
- The README template can be found here
- Don’t forget to follow the design and clarity guide. This is graded!
After completing the homework, you will submit:
Because all we need are the files, you do not need to submit the whole project folder. As long as the file you create has the same name as what needs to be submitted, you’re good to go!
If you are using late days, make sure to make a note of that in your readme. Remember, you may only use a maximum of 3 late days per assignment. If the assignment is late (and you do NOT have anymore late days) no credit will be given.
Helpful Things
Quick Syntax
Rounding
You can round a number in Python using the round
function. The round
function has two arguments, the first being the number to round and the second representing how many decimal places to have. Here are a few examples:
# If you'd like to round to the nearest whole number
# Note: this will still produce a decimal not an Int
round(1.3446, 0) # produces 1.0
round(1.5111, 0) # produces 2.0
round(1.7934, 0) # produces 2.0
# If you'd like to round to the nearest tenth
round(1.3446, 1) # produces 1.3
round(1.5111, 1) # produces 1.5
round(1.7934, 1) # produces 1.8
# If you'd like to round to the nearest hundreth
round(1.3446, 2) # produces 1.34
round(1.5111, 2) # produces 1.51
round(1.7934, 2) # produces 1.79
round(1.799, 2) # produces 1.8
Documentation
The Assignment
Detective Pikachu’s Election
Write your solution for this file in election.py
. Check out this how to get started document on how to create files and write simple scripts.
An election is happening in the Kanto region, and Detective Pikachu needs help counting the votes (Bulbasaur and Charmander are making a mess in the election between Al Blood and George Shrub). Help Detective Pikachu ensure a peaceful transition of power!
- Write a function
name_matches
that takes a vote
(string) and a name
(string) and returns a boolean indicating whether the vote is for the given name, regardless of how the vote and name have been capitalized (that is, ignore case). You can lowercase a string s
in Python by writing s.lower()
(where s
gets replaced by the string you want to lowercase). As in Pyret, you have Boolean operators and
, or
, and not
.
- Write a function
any_votes_for
that takes a name
(string) and a votes
, a list of votes, and returns a boolean indicating whether any votes were for the given name (case insensitive).
- Write a function
count_votes_for
that takes a name
(string) and a votes
, a list of votes, and returns the number of votes that were for the given name (case insensitve).
- Write a function
got_more_votes
that takes two names, name_one
and name_two
, and votes
, a list of votes, and returns the name that got more votes (case insensitve). If there is a tie, return the string “Tie”
.
- Write a function
record_vote
that takes in votes
, a list of votes, and new_vote
, a new vote, and adds the vote to the end of the list.
- Write a function
percentage_votes
that takes a name and a list of votes and returns the percentage of votes that match (case insensitve) the given name. You should return the percent in decimal form (i.e. if the name matches 90% of the votes, return 0.9). Please round to the nearest tenth. Don’t forget to consider the case where someone has no votes (or where there are no votes)!
- Write a function
test_votes
to test your vote functions. In particular, think about how to test record_vote
. Run test_votes()
to make sure all your tests pass and your functions work as expected.
Any time you are returning a name, it may be returned lowercased or uppercased. Our test suite is case insensitive, so returning "Jennifer"
will be treated the same as "jennifer"
and "JENNIFER"
. Same applies for functions that return lists. ["Jen","Jennifer"]
will be treated the same as ["jen","JENNIFER"]
.
Homework 8: Python Practice with Detective Pikachu
Due: Tuesday November 12, 2019 at 9:00PM EST.

Handin
After completing the homework, you will submit:
README.txt
election.py
Because all we need are the files, you do not need to submit the whole project folder. As long as the file you create has the same name as what needs to be submitted, you’re good to go!
If you are using late days, make sure to make a note of that in your readme. Remember, you may only use a maximum of 3 late days per assignment. If the assignment is late (and you do NOT have anymore late days) no credit will be given.
Helpful Things
Quick Syntax
Rounding
You can round a number in Python using the
round
function. Theround
function has two arguments, the first being the number to round and the second representing how many decimal places to have. Here are a few examples:Documentation
The Assignment
Detective Pikachu’s Election
Write your solution for this file in
election.py
. Check out this how to get started document on how to create files and write simple scripts.An election is happening in the Kanto region, and Detective Pikachu needs help counting the votes (Bulbasaur and Charmander are making a mess in the election between Al Blood and George Shrub). Help Detective Pikachu ensure a peaceful transition of power!
name_matches
that takes avote
(string) and aname
(string) and returns a boolean indicating whether the vote is for the given name, regardless of how the vote and name have been capitalized (that is, ignore case). You can lowercase a strings
in Python by writings.lower()
(wheres
gets replaced by the string you want to lowercase). As in Pyret, you have Boolean operatorsand
,or
, andnot
.any_votes_for
that takes aname
(string) and avotes
, a list of votes, and returns a boolean indicating whether any votes were for the given name (case insensitive).count_votes_for
that takes aname
(string) and avotes
, a list of votes, and returns the number of votes that were for the given name (case insensitve).got_more_votes
that takes two names,name_one
andname_two
, andvotes
, a list of votes, and returns the name that got more votes (case insensitve). If there is a tie, return the string“Tie”
.record_vote
that takes invotes
, a list of votes, andnew_vote
, a new vote, and adds the vote to the end of the list.percentage_votes
that takes a name and a list of votes and returns the percentage of votes that match (case insensitve) the given name. You should return the percent in decimal form (i.e. if the name matches 90% of the votes, return 0.9). Please round to the nearest tenth. Don’t forget to consider the case where someone has no votes (or where there are no votes)!test_votes
to test your vote functions. In particular, think about how to testrecord_vote
. Runtest_votes()
to make sure all your tests pass and your functions work as expected.Any time you are returning a name, it may be returned lowercased or uppercased. Our test suite is case insensitive, so returning
"Jennifer"
will be treated the same as"jennifer"
and"JENNIFER"
. Same applies for functions that return lists.["Jen","Jennifer"]
will be treated the same as["jen","JENNIFER"]
.