Implement an election simulator.

Implement an election simulator.

Background

In a first-past-the-post electorial system (as is used in federal and provincial elections here in Canada), a jurisdiction is split into multiple constituencies (commonly called ridings in Canada), each of which sends one representative to the legislature (Parliament, House of Assembly, Legislative Assembly, etc.).

Objective

You will write a Python script that does N things:

  1. Input a number of constituencies / parliamentary seats

  2. Inputs a list of political party names from the user

  3. Prints these party names, in the same order they were given

  4. Elect a representative for each seat

  5. Output the seats won by each party

Assignment submissions that can do all of these things according to the specifications below will receive full marks. Submissions that do some of these things, or that do them in ways that don’t fully conform to the specification, will receive partial credit. My suggestion is to start by focusing on part of the problem rather than trying to do it all at once. It is much better to have a submission that correctly implements some of the functionality than to have one that incorrectly attempts it all!

Warning

Reminder: assignments in this course are individual work. When you submit your assignment, you should be able to honestly say, "I did this myself". You are free to discuss the course content, lecture modules, etc., with your peers, but not your assignment solutions.

Input constituencies

The first thing your script should do is input the number of constituencies / ridings / parliamentary seats. This will be used later. Prompt the user with the string "Constituencies: " ; you may assume that the user will respond with a positive integer. For example, when I (or the autograder) run your program, I might simulate the 2021 Federal election by entering 338 as the number of constituencies (since there are 338 seats in the House of Commons representing 338 ridings). Or, to simulate an election in Newfoundland and Labrador, I might enter the number 40 when prompted, as there are 40 seats in the House of Assembly representing 40 ridings / constituencies.

Input parties

The next thing your script should do is input some names of political parties. Your code should prompt the user with some meaningful prompt (e.g., `Party name: `) and store the name that the user provides. This process should repeat until the user enters an empty string (i.e., presses Enter without typing a name).

Print parties

Next, your code should print out the number and names of the parties that the user has entered. This should include the string N parties:, where N is replaced by the number of parties. Each party should then be printed on a separate line, optionally with a bullet for each party. For example, if the user entered the five parties that won seats in the 2021 federal election, your program should output:

5 parties:
 - Liberal Party of Canada
 - Conservative Party of Canada
 - Bloc Québécois
 - New Democratic Party
 - Green Party of Canada

Elect representatives

Next, your code should run the election for each constituency. For each constitutency (based on the number given above), the code should:

  1. Prompt the user for the constituency’s name.

  2. Prompt the user for the number of votes received by each party in that constituency, asking about each party in the order that they were entered above.

  3. Output which party won the riding, using both the name of the riding that the user entered and the full name of the party. In the event of a tie, you may declare either party to be the winner.

For example, using the results of the 2021 federal election in the ridings of St. John’s East and St. John’s South–Mount Pearl, the interaction between a user and the program might look like:

Constituency: St. John's East
Liberal Party of Canada: 17239
Conservative Party of Canada: 7119
Bloc Québécois: 0
New Democratic Party: 13090
Green Party of Canada: 0
People's Party of Canada: 723
The winner in St. John's East is the Liberal Party of Canada

Constituency: St. John's South–Mount Pearl
Liberal Party of Canada: 19478
Conservative Party of Canada: 6447
Bloc Québécois: 0
New Democratic Party: 8113
Green Party of Canada: 0
People's Party of Canada: 638
The winner in St. John's South–Mount Pearl is the Liberal Party of Canada

Output total seats

Finally, your code should output the string Total seats won: followed by how many seats each party won, with each line showing the name of a party and followed by the number of seats it won. Again, using the 2021 federal election as an example, your output should read:

Total seats won:
Liberal Party of Canada : 157
Conservative Party of Canada : 121
Bloc Québécois : 32
New Demogratic Party : 25
Green Party of Canada : 2

Submission

Submit your work to Gradescope in a file called election.py. You will get as many attempts as you need until the deadline, but some of the autograder test cases may be held in reserve until the deadline. Thus, you may not see your full score until the deadline has passed.