Question

PYTHON (A voting machine consists of an election district, together with the names of the candidates...

PYTHON

(A voting machine consists of an election district, together with the names of the candidates and the number of votes cast for each candidate.

Define a VotingMachine class, wrote a docstring for the class, and define the following three class methods:

1. An initialization method. The initialization method should:

* take a string parameter, electionDistrict, and assign it to the instance attribute elctionDistrict of the voting machine being created.

* create an instance attribute named candidates for the voting machine being created and initialize it to the empty dictionary. Each key in candidates will be a candidate and the corresponding value will be the number of votes the candidate receives.

2. A method named castVote. This method castVote should take a string parameter, candidate. The method castVote should update the candidates dictionary by incrementing the vote for each candidate by one. The method castVote should add candidate as a key if it is not present.

3. A method named getVoteCount. This method should return the sum of votes cast for all candidates in the election district.)

Assume that correct code for the class votingMachine has been saved in a file named voting_machine.py. Write a code that uses the class and method definitions to perform the following tasks:

1. import the module that defines the class votingmachine

2. create a voting machine for an election district named Gotham City

3. cast one vote for Hamilton Hill and another for Arthur Reeves in Gotham City election district

4. print out the Gotham City election district vote count

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#python code

#voting_machine.py

class VotingMachine:
    def __init__(self, electionDistrict):
        self.electionDistrict = electionDistrict
        self.candidates = {}

    def castVote(self, candidate):
        if candidate in self.candidates:
            self.candidates[candidate] += 1
        else:
            self.candidates[candidate] = 1


    def getVoteCount(self):
        sum = 0
        for key in self.candidates:
            sum += self.candidates[key]
        return sum

#==========================

#Main.py

import voting_machine
v = voting_machine.VotingMachine("Gotham City")
v.castVote("Hamilton Hill")
v.castVote("Arthur Reeves")

print("Gotham City election district vote count: ", v.getVoteCount())

#screenshot for indentation help

#output

//If you need any help regarding this solution.... please leave a comment........ thanks

Add a comment
Know the answer?
Add Answer to:
PYTHON (A voting machine consists of an election district, together with the names of the candidates...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • To combat election fraud, your city is instituting a new voting procedure. The ballot has a...

    To combat election fraud, your city is instituting a new voting procedure. The ballot has a letter associated with every selection a voter may make. A sample ballot is shown:- (Voter places a tick next to his or her preferred candidate, proposition and measure to indicate his/her vote) 1. Mayoral Candidates     A. Pincher, Penny     B. Dover, Skip     C. Perman, Sue 2. PROP 17     D. YES     E. NO 3. MEASURE 1     F. YES     G....

  • Write a Python program that creates a class which represents a candidate in an election. The...

    Write a Python program that creates a class which represents a candidate in an election. The class must have the candidates first and last name, as well as the number of votes received as attributes. The class must also have a method that allows the user to set and get these attributes. Create a separate test module where instances of the class are created, and the methods are tested. Create instances of the class to represent 5 candidates in the...

  • Write a program that supports the three phases (setup, voting and result-tallying) which sets up and...

    Write a program that supports the three phases (setup, voting and result-tallying) which sets up and executes a voting procedure as described above. In more details: You can start with the given skeleton (lab6_skeleton.cpp). There are two structures: Participant structure, which has the following members: id -- an integer variable storing a unique id of the participant (either a candidate or a voter). name -- a Cstring for storing the name of the participant.. hasVoted -- a boolean variable for...

  • I need help with the adding the following to my python code (also provided below) M5A6Part2...

    I need help with the adding the following to my python code (also provided below) M5A6Part2 ⦁   When a user successfully votes, add the user’s voterID to the _voterIDsVoted list ⦁   When a user successfully votes, increment the value of the variable that counts the votes for the candidate the user voted for. This would be either _candidateAVotes or _candidateBVotes Hint: run your original test code again. If you reused a valid voter ID in your tests it should cause...

  • (2 bookmarks) In JAVA You have been asked to write a program that can manage candidates...

    (2 bookmarks) In JAVA You have been asked to write a program that can manage candidates for an upcoming election. This program needs to allow the user to enter candidates and then record votes as they come in and then calculate results and determine the winner. This program will have three classes: Candidate, Results and ElectionApp Candidate Class: This class records the information for each candidate that is running for office. Instance variables: first name last name office they are...

  • In November 1993, the state of Pennsylvania conducted clections for its state legislature. The re...

    In November 1993, the state of Pennsylvania conducted clections for its state legislature. The result in the Senate election in the 2nd district (based in Philadelphia) was challenged in court and ultimately overturned. The Democratic candidate won 19,127 of the votes cast by voting machine, while the Republican won 19,691 votes cast by voting machine, giving the Republican a lead of 564 votes. However, the Democrat won 1,391 absentee ballots, while the Republican won just 366 absentee ballots, more than...

  • 15.1: Tallying Votes Elections are a formal group-decision making process by which a population chooses a...

    15.1: Tallying Votes Elections are a formal group-decision making process by which a population chooses a person to hold office, such as the mayor of a city. Another use of elections is to accept or reject a political proposition. Most election results are tallied, or counted, using electronic voting. Most electronic voting machines use a computer to take care of the chore of casting and counting votes. This Tuesday, November 6 (the date your assignment is due) is election day....

  • * The Map class is used to create and manipulate voting maps. The value of a...

    * The Map class is used to create and manipulate voting maps. The value of a * cell on the map denotes the party for which the majority of the population * of that cell votes for. For instance, in the following map, PARTY_X is the * choice of voters in three cells, while PARTY_O is preferred in the rest of * the map: * O X O * X O X * O O O * A map is...

  • python 3 inheritance Define a class named bidict (bidirectional dict) derived from the dict class; in...

    python 3 inheritance Define a class named bidict (bidirectional dict) derived from the dict class; in addition to being a regular dictionary (using inheritance), it also defines an auxiliary/attribute dictionary that uses the bidict’s values as keys, associated to a set of the bidict’s keys (the keys associated with that value). Remember that multiple keys can associate to the same value, which is why we use a set: since keys are hashable (hashable = immutable) we can store them in...

  • A) Please implement a Python script to define a student class with the following attributes (instance...

    A) Please implement a Python script to define a student class with the following attributes (instance attributes): cwid: student’s CWID first_name : student’s first name last_name: student’s last name gender: student’s gender gpa: student’s gpa Please make these attributes as private ones so they have to be accessed via getter/setter methods or property. For this purpose, please define a getter/setter method and property for each of the attributes. Another thing you need to do is to define a constructor that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT