Question

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 election, then it should output each candidate’s name, the votes received by that candidate, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election.

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

# This programs is written for python version greater than 3.0

class electionCandidate:
""" Class for Election Candidate """
def __init__(self, fName, lName, votesRcvd):
self.fName = fName
self.lName = lName
self.votesRcvd = votesRcvd

#Methods to set the attributes are commented as in this case constructor is used to set attributes
#def setName(self, fName, lName):
# self.fName = fName
# self.lName = lName

#def setVotesRcvd(self, votesRcvd):
# self.votesRcvd = votesRcvd
  
def getName(self):
return (self.fName+' '+self.lName)

def getVotesRcvd(self):
return (self.votesRcvd)
  
def main():
#Create instances for candidates
candidateList = []
candidateList.append(electionCandidate("fName1", "lName1", 10))
candidateList.append(electionCandidate("fName2", "lName2", 20))
candidateList.append(electionCandidate("fName3", "lName3", 75))
candidateList.append(electionCandidate("fName4", "lName4", 45))
candidateList.append(electionCandidate("fName5", "lName5", 50))

totalVotes = 0
prevVotes = 0
winner = ""
for candidate in candidateList:
totalVotes += candidate.getVotesRcvd()
if (candidate.getVotesRcvd() > prevVotes):
winner = candidate.getName()
prevVotes = candidate.getVotesRcvd()

print("Print all candidate name")
for candidate in candidateList:
print("Name:" , candidate.getName(), ",and % of Votes is:", (candidate.getVotesRcvd()/totalVotes)*100,"%")

print()
print("Winner name of election is")
print(winner)

  
# Run the main function. This should be the last statement in the file.
main()

*************************************************
Output of Program is:
Print all candidate name
Name: fName1 lName1 ,and % of Votes is: 5.0 %
Name: fName2 lName2 ,and % of Votes is: 10.0 %
Name: fName3 lName3 ,and % of Votes is: 37.5 %
Name: fName4 lName4 ,and % of Votes is: 22.5 %
Name: fName5 lName5 ,and % of Votes is: 25.0 %

Winner name of election is
fName3 lName3

Add a comment
Know the answer?
Add Answer to:
Write a Python program that creates a class which represents a candidate in an election. The...
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
  • (Write a program for C++ )that allows the user to enter the last names of five...

    (Write a program for C++ )that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is: Candidate    Votes Received    % of Total Votes Johnson            5000               25.91...

  • Write a program that allows the user to enter the last names of five candidates in...

    Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate's name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is: Candidate Votes Received % of Total Votes Johnson 5000 25.91 Miller 4000 20.73 Duffy...

  • c++ Votes received by the candidate. Your program should also output the winner of the election....

    c++ Votes received by the candidate. Your program should also output the winner of the election. A sample output is: The Winner of the Election is Duffy. The input data should come from the file. Use 3 separate arrays to store the names, votes, and percentages before they are displayed. Include functions inputValues to input values for candidate names and votes received and place them in arrays. calcPercents which fills an array with the percentage of votes each candidate got....

  • 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...

  • COSC 1437 C++ Project Assignment 2 Poll Summary Utilize a dynamic array of structure to summarize...

    COSC 1437 C++ Project Assignment 2 Poll Summary Utilize a dynamic array of structure to summarize votes for a local election Specification: Write a program that keeps track the votes that each candidate received in a local election. The program should output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. To solve this problem, you will use one dynamic...

  • Using C language, write a program for the following: In a student representative council election, there...

    Using C language, write a program for the following: In a student representative council election, there are ten (10) candidates. A voter is required to vote a candidate of his/her choice only once. The vote is recorded as a number from 1 to 10. The number of voters are unknown beforehand, so the votes are terminated by a value of zero (0). Votes not within and not inclusive of the numbers 1 to 10 are invalid (spoilt) votes. A file,...

  • Java program Candidate Class: This class records the information for each candidate that is running for...

    Java program Candidate Class: This class records the information for each candidate that is running for office. Instance variables: first name last name office they are running for the party they represent total votes a boolean value to record if they won Methods: Custom constructor that accepts first and last name, office and party Custom constructor that accepts an instance of another customer Getters for all instance variables and setters for total votes and the boolean variable toString: This method...

  • (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...

  • It’s almost election day and the election officials need a program to help tally election results....

    It’s almost election day and the election officials need a program to help tally election results. There are two candidates for office—Polly Tichen and Ernest Orator. The program’s job is to take as input the number of votes each candidate received in each voting precinct and find the total number of votes for each. The program should print out the final tally for each candidate—both the total number of votes each received and the percent of votes each received. Clearly...

  • It's almost election day and the election officials need a program to help tally election results....

    It's almost election day and the election officials need a program to help tally election results. There are two candidates for office—Polly Tichen and Ernest Orator. The program's job is to take as input the number of votes each candidate received in each voting precinct and find the total number of votes for each. The program should print out the final tally for each candidate—both the total number of votes each received and the percent of votes each received. Clearly...

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