- Read the question carefully (twice).
- Break the task into the smallest steps.
- Sketch or write pseudocode before coding.
- Start small — test as you go.
- Check your solution with different cases.
Overview:
In many elections, people vote for a candidate by writing their name. A simple voting system counts how many votes each candidate receives and then determines the winner.
Task 1: Count the Votes
Write a program that takes a list of votes (each vote is the name of a candidate) and counts how many votes each candidate received.
Example input:
votes = ["Alice", "Bob", "Alice", "Eve", "Bob", "Alice"]
Expected output:
Alice: 3
Bob: 2
Eve: 1
Task 2: Find the Winner
Extend your program to determine who won the election.
Expected output (using the list above):
The winner is Alice with 3 votes.
Task 3: Handle Ties
Modify your program so that if two or more candidates have the same highest number of votes, the program shows all of them.
Example input:
votes = ["Alice", "Bob", "Alice", "Bob"]
Expected output:
Alice: 2
Bob: 2
It is a tie between Alice and Bob!
Task 4: Test with an Empty List
What happens if the list of votes is empty ([])?
Update your program to print:
No votes cast.
Task 5: Invalid Votes
Sometimes people make mistakes. Assume only ["Alice", "Bob", "Eve"] are valid candidates. Update your program to ignore any other names.
Example input:
votes = ["Alice", "Bob", "Charlie", "Alice", "Eve", "Unknown"]
Expected output:
Alice: 2
Bob: 1
Eve: 1
Ignored votes: 2
The winner is Alice with 2 votes.
Testing Checklist
- Program counts votes correctly for multiple candidates.
- Program announces the correct winner.
- Program handles ties.
- Program handles no votes.
- Program ignores invalid votes.