Python question Election contest We will use the term contes
Python question.
Election contest. We will use the term contest to describe a single competition in an election. For example, in an election 1of a U-S. Senator, al the candidates for a single Senator seat participate in a single contest. Implement the following class, which represents a single contest in a election. A competition for a single political position in an election. posion. str, The political position being sought in this contest. cands. dict, A dictionary of candidates for this election. They key for each candidate is a string representing that candidate\'s name and the value for that key holds they tally (number) of votes for that candidate. A string specifying the position, and a list of strings representing candidates\' names. A new object of type Contest is created. The value arg 1 is assigned to the state variable position, and a new dictionary with one key for each element of arg 2 is assigned to the stage variable cands. Each key in cands receives an initial value of 0 votes That newly created contest Object. get position String, the value of the state variable position. get Candidates A dictionary, the value of the state variable cands. get Tally string, the name of a candidate Integer, the number of votes for the candidate named arg1. A string, representing a candidates name, and an integer, representing a number of votes. The number arg 2 is added to the tally for the candidate named arg1 in the state variable cand. The updated tally for the candidate arg1. sen = Contest(\'Senator\', (\'\'Jones, \'Smith\')) sen.getCandidates() rightarrow (\'Jones\' 0, Smith\'0) sen. add Tally (\'Smith\', 505) sen.addTally (\'Jones\', 716) sen.getTally(\'Smith\') rightarrow 505 sen.addTally(\'Smith\', 290) sen.getCandidates()rightarrow (\'Jones 716\'\'Smith 795\')Solution
Please find the required program along with its output. Please see the comments against each line to understand the step.
class Contest:
position = \"\"
cands = { }
def __init__(self, pos, cnds): #constructor using position and list of candidates
self.position = pos
for c in cnds:
self.cands[c]=0
def getPosition(self): #getter method for position
return self.position
def getCandidates(self): #getter method for cands
return self.cands
def getTally(self,name): #tally getter method
for c in self.cands:
if(c==name):
return self.cands[name]
def addTally(self,name,tally): #tally getter method
for c in self.cands:
if(c==name):
self.cands[name] = self.getTally(name) + tally
return
self.cands[name] = tally
sen = Contest(\'Senator\',[\'Jones\',\'Smith\']) #tests
print(sen.getCandidates())
sen.addTally(\'Smith\',505)
sen.addTally(\'Jones\',716)
print(sen.getTally(\'Smith\'))
sen.addTally(\'Smith\',290)
print(sen.getCandidates())
-----------------------------------------------------------
OUTPUT:
