I;m a beginner programming student learning python and I;ve been troubleshooting for hours and can;t figure out what to change [closed]

  Kiến thức lập trình

The instructions I was given go as followed:
A. Write a complete and runnable Python program (code) which will record the votes for one of two candidates for each of president and vice president, by using the Python class VoteRecorder, which you will design and create/write. This class must be in a file VoteRecorder.py by itself. VoteRecorder class will have class/static variables to keep track of the total votes for each of the candidates and instance variables to keep track of the votes made by a single voter. This class will be used as resource/service provider for the VoteRecorderDemo class, which will conduct the election process. This class must be in a file VoteRecorderDemo.py by itself (not in the same file VoteRecorder.py which contains the class VoteRecorder).

  1. Here are the class static/class variables:

votes_candidate_president_1—an integer(int) that holds the number of votes for the first candidate for president.
votes_candidate_president_2—an integer(int) that holds the number of votes for the second candidate for president.
votes_candidate_vice_president_1—an integer(int) that holds the number of votes for the first candidate for vice president.
votes_candidate_vice_president_2—an integer(int) that holds the number of votes for the second candidate for vice president.

  1. Here are the class Instance variables:

name_candidate_president_1—a string (str) that holds the name of the first candidate for president.
name_candidate_president_2—a string (str) that holds the name of the second candidate for president.
name_candidate_vice_president_1—a string (str) that holds the name of the first candidate for vice president.
name_candidate_vice_president_2—a string (str) that holds the name of the second candidate for vice president.
my_vote_for_president—an integer (int) that holds the vote of a single individual for president (0 for no choice, 1 for the first candidate, and 2 for the second candidate).
my_vote_for_vice_president—an integer (int) that holds the vote of a single individual for vice president (0 for no choice, 1 for the first candidate, and 2 for the second candidate).

  1. Her are the class constructor and Instance Methods:

In addition of having appropriate class constructor to contain class Instance variables, VoteRecorder has the following Instance methods:

setCandidatesPresident(string name_1, string name_2)—an Instance method that sets the names of the two candidates for president. It does not return anything.
string getCurrentVotePresident()—an Instance method that returns a string with the current total number of votes for both presidential candidates.
setCandidatesVicePresident(string name_1, string name_2)—an Instance method that sets the names of the two candidates for vice president. It does not return anything.
String getCurrentVoteVicePresident()—an Instance method that returns a string with the current total number of votes for both vice presidential candidates.
resetVotes()—an Instance method that resets the vote counts to zero. It does not return anything.
recordVotes()—an Instance method that will add a voter’s votes to the appropriate static/class variables of the object for each voter. It does not return anything.
int getAVote(string name_1, string name_2)—an Instance method that returns a vote choice for a single race from an individual (0 for no choice, 1 for the first candidate, and 2 for the second candidate).
getVotes()—an Instance method which gets a vote choice for president and vice president from a voter. And records the voter votes for president and vice president. It does not return anything.
boolean confirmVotes()—an Instance method that displays a person’s vote for president and vice president, asks whether the voter is happy with these choices, and returns True or False according to a voter of yes or no response.
getAndConfirmVotes()—an Instance method that gets an individual’s votes for president and vice president, confirms them, and then records them. It does not return anything.

B. More Implementation Guidelines:

Create a class, VoteRecorderDemo, which must contain a main() method that will conduct an election process. This class must be in a file by itself, not inside the same file which contains the VoteRecorder class. The following activities must be done in this class:

Import the module/file which contains VoteRecorder class, for the VoteRecorderDemo class to be able to use the data and services provided by the VoteRecorder class. Usually, the import statement must be put/placed at the beginning of the file and outside any method. Note: do not place import statements inside the main method.
Create a new VoteRecorder class object.
Initialize (Note: do not prompt the user to enter the names of the candidates) the candidates for president are set to Annie and Bob, using the created object in step 1 above.
Initialize (Note: do not prompt the user to enter the names of the candidates) the candidates for vice president are set to John and Susan, using the created object in step 1 above.
Reset the votes by calling resetVotes method of VoteRecorder class, using the created object in step 1 above.
Set a loop to process the voting of as many voters that are available. Note: Do not set the loop for a fixed or specific number of voters.
For voting, create a new VoteRecorder object for each voter.
Process a voter by calling the getAndConfirmVotes method of VoteRecorder class, using the created object in step 6 above. getAndConfirmVotes method calls other methods of VoteRecorder class to get and record the voter voting input.
Display/Output the voter voting result. See Sample Program Run below, for how to display.
Prompt, if there are other voters. If yes, then repeat steps 6 through 8, to process the new voter. Otherwise, terminate the loop/end the voting process. Go to step 10, below to report voting outcome.
After all the voters are done, present/display the total votes each candidate received. Display 0 (zero), if a candidate did not receive any votes. See Sample Program Run below, for how to display.
And these are sample outputs my professor wants to see:

YOU ARE VOTING FOR PRESIDENT
Please choose a candidate:
0 – No one
1 – Annie
2 – Bob
0
YOU ARE VOTING FOR VICE PRESIDENT
Please choose a candidate:
0 – No one
1 – John
2 – Susan
1
Your vote for president is no one
Your vote for vice president is John
Type yes if you are happy with your vote
yes
Type yes if there is another voter
yes
YOU ARE VOTING FOR PRESIDENT
Please choose a candidate:
0 – No one
1 – Annie
2 – Bob
2
YOU ARE VOTING FOR VICE PRESIDENT
Please choose a candidate:
0 – No one
1 – John
2 – Susan
2
Your vote for president is Bob
Your vote for vice president is Susan
Type yes if you are happy with your vote
no
YOU ARE VOTING FOR PRESIDENT
Please choose a candidate:
0 – No one
1 – Annie
2 – Bob
1
YOU ARE VOTING FOR VICE PRESIDENT
Please choose a candidate:
0 – No one
1 – John
2 – Susan
1
Your vote for president is Annie
Your vote for vice president is John
Type yes if you are happy with your vote
yes
Type yes if there is another voter
no
Annie has 1 votes; Bob has 0 votes;
John has 2 votes; Susan has 0 votes;

Sample Run 2: for invalid data values

YOU ARE VOTING FOR PRESIDENT

Please choose a candidate:

  0 - No one

  1 - Annie

  2 - Bob

5

Please choose a candidate:

  0 - No one

  1 - Annie

  2 - Bob

200

Please choose a candidate:

  0 - No one

  1 - Annie

  2 - Bob

1

YOU ARE VOTING FOR VICE PRESIDENT

Please choose a candidate:

  0 - No one

  1 - John

  2 - Susan

7

Please choose a candidate:

  0 - No one

  1 - John

  2 - Susan

9

Please choose a candidate:

  0 - No one

  1 - John

  2 - Susan

1

Your vote for president is Annie

Your vote for vice president is John

Type yes if you are happy with your vote

no

YOU ARE VOTING FOR PRESIDENT

Please choose a candidate:

  0 - No one

  1 - Annie

  2 - Bob

7

Please choose a candidate:

  0 - No one

  1 - Annie

  2 - Bob

1

YOU ARE VOTING FOR VICE PRESIDENT

Please choose a candidate:

  0 - No one

  1 - John

  2 - Susan

1

Your vote for president is Annie

Your vote for vice president is John

Type yes if you are happy with your vote

yes

Type yes if there is another voter

yes

YOU ARE VOTING FOR PRESIDENT

Please choose a candidate:

  0 - No one

  1 - Annie

  2 - Bob

8

Please choose a candidate:

  0 - No one

  1 - Annie

  2 - Bob

2

YOU ARE VOTING FOR VICE PRESIDENT

Please choose a candidate:

  0 - No one

  1 - John

  2 - Susan

3

Please choose a candidate:

  0 - No one

  1 - John

  2 - Susan

2

Your vote for president is Bob

Your vote for vice president is Susan

Type yes if you are happy with your vote

yes

Type yes if there is another voter

no

Annie has 1 votes; Bob has 1 votes;

John has 1 votes; Susan has 1 votes;

And this is the code I’ve written so far:
class VoteRecorder:
“””
A class to record votes for candidates.

Attributes:
    name_candidate_president_1 (str): The name of the first candidate for president.
    name_candidate_president_2 (str): The name of the second candidate for president.
    name_candidate_vice_president_1 (str): The name of the first candidate for vice president.
    name_candidate_vice_president_2 (str): The name of the second candidate for vice president.
    my_vote_for_president (int): The vote of a single individual for president (0 for no choice, 1 for the first candidate, and 2 for the second candidate).
    my_vote_for_vice_president (int): The vote of a single individual for vice president (0 for no choice, 1 for the first candidate, and 2 for the second candidate).
    votes_candidate_president_1 (int): The number of votes for the first candidate for president.
    votes_candidate_president_2 (int): The number of votes for the second candidate for president.
    votes_candidate_vice_president_1 (int): The number of votes for the first candidate for vice president.
    votes_candidate_vice_president_2 (int): The number of votes for the second candidate for vice president.
"""

votes_candidate_president_1 = 0
votes_candidate_president_2 = 0
votes_candidate_vice_president_1 = 0
votes_candidate_vice_president_2 = 0

def __init__(self):
    """
    Initializes a VoteRecorder object.
    """
    self.name_candidate_president_1 = ""
    self.name_candidate_president_2 = ""
    self.name_candidate_vice_president_1 = ""
    self.name_candidate_vice_president_2 = ""
    self.my_vote_for_president = 0
    self.my_vote_for_vice_president = 0



def setCandidatesPresident(self, name_1, name_2):
    """
    Sets the names of the candidates for president.

    Args:
        name_1 (str): The name of the first candidate for president.
        name_2 (str): The name of the second candidate for president.
    """
    self.name_candidate_president_1 = name_1
    self.name_candidate_president_2 = name_2

def getCurrentVotePresident(self):
    """
    Gets the current total number of votes for both presidential candidates.

    Returns:
        str: A string with the current total number of votes for both presidential candidates.
    """
    return f'Votes for {self.name_candidate_president_1}: {self.votes_candidate_president_1},' 
           f' Votes for {self.name_candidate_president_2}:{self.votes_candidate_president_2}'

def setCandidatesVicePresident(self, name_1, name_2):
    """
    Sets the names of the candidates for vice president.

    Args:
        name_1 (str): The name of the first candidate for vice president.
        name_2 (str): The name of the second candidate for vice president.
    """
    self.name_candidate_vice_president_1 = name_1
    self.name_candidate_vice_president_2 = name_2

def getCurrentVoteVicePresident(self):
    """
    Gets the current total number of votes for both vice presidential candidates.

    Returns:
        str: A string with the current total number of votes for both vice presidential candidates.
    """
    return f'Votes for {self.name_candidate_vice_president_1}: {self.votes_candidate_vice_president_1}' 
           f' Votes for {self.name_candidate_vice_president_2}: {self.votes_candidate_vice_president_2}'

def resetVotes(self):
    """
    Resets the vote counts to zero.
    """
    self.votes_candidate_president_1 = 0
    self.votes_candidate_president_2 = 0
    self.votes_candidate_vice_president_1 = 0
    self.votes_candidate_vice_president_2 = 0

def recordVotes(self):
    """
    Records a voter’s votes by adding them to the appropriate vote counts.
    """
    if self.my_vote_for_president == 1:
        self.votes_candidate_president_1 += 1
    elif self.my_vote_for_president == 2:
        self.votes_candidate_president_2 += 1

    if self.my_vote_for_vice_president == 1:
        self.votes_candidate_vice_president_1 += 1
    elif self.my_vote_for_vice_president == 2:
        self.votes_candidate_vice_president_2 += 1

def getAVote(self, name_1, name_2, position):
    """
    Prompts the user to vote for a candidate.

    Args:
        name_1 (str): The name of the first candidate.
        name_2 (str): The name of the second candidate.
        position (str): The position for which the vote is being cast.

    Returns:
        int: The user's vote choice (0 for no choice, 1 for the first candidate, and 2 for the second candidate).
    """
    print(f'YOU ARE VOTING FOR {position.upper()}')
    print(f'Please choose a candidate for {position}:')
    print('   0 - No one')
    print(f'   1 - {name_1}')
    print(f'   2 - {name_2}')

    while True:
       vote = int(input())
       if vote in [0,1,2]:
               return int(vote)
       else:
           print(f'YOU ARE VOTING FOR {position.upper()}')
           print('Please choose a candidate:')
           print('   0 - No one')
           print(f'   1 - {name_1}')
           print(f'   2 - {name_2}')

def getVotes(self):
    """
    Gets a voter's vote choices for president and vice president.
    """
    self.my_vote_for_president = self.getAVote(self.name_candidate_president_1, self.name_candidate_president_2, "president")
    self.my_vote_for_vice_president = self.getAVote(self.name_candidate_vice_president_1, self.name_candidate_vice_president_2, "vice president")

def confirmVotes(self):
    """
    Displays a voter's vote choices for president and vice president, asks for confirmation,
    and returns True if the voter is happy with their choices, False otherwise.

    Returns
    -------
    None.

    """
    
    if self.my_vote_for_president == 0:
        print('  - No one')
    elif self.my_vote_for_president == 1:
        print (f'  - {self.name_candidate_president_1}')
    elif self.my_vote_for_president == 2:
        print(f"  - {self.name_candidate_president_2}")
        
    if self.my_vote_for_vice_president == 0:
        print('  - No one')
    elif self.my_vote_for_vice_president == 1:
        print (f' - {self.name_candidate_vice_president_1}')
    elif self.my_vote_for_vice_president == 2:
        print(f"  - {self.name_candidate_vice_president_2}")
    
    while True:
        response = input('Type yes if you are happy with your voten').lower()
        if response == 'yes':
            return True
        elif response == 'no':
            self.resetVotes()
            
    
    return False
    
def getAndConfirmVotes(self):
    """
    Gets a voter's vote choices, confirms them, and records the votes.
    """
    self.getVotes()
    if self.confirmVotes():
        self.recordVotes()

And:from VoteRecorder import VoteRecorder

class VoteRecorderDemo:
“””
A class to conduct an election process using the VoteRecorder class.
“””

@staticmethod
def main():
    """
    Main method to conduct the election process.
    """
    # Create a new VoteRecorder class object
    recorder = VoteRecorder()

    # Initialize candidates for president and vice president
    recorder.setCandidatesPresident("Annie", "Bob")
    recorder.setCandidatesVicePresident("John", "Susan")

    # Reset the Votes
    recorder.resetVotes()

    # Voting loop
    voters_available = True
    while voters_available:
        # Process a voter and get the candidate names voted for
        recorder.getAndConfirmVotes()

        # Display voting result for each voter
        print(f'Your vote for president is: {recorder.my_vote_for_president}')
        print(f'Your vote for vice president is: {recorder.my_vote_for_vice_president}')

        # Prompt for more voters
        if input('Type yes if there is another votern').lower() != 'yes':
            break

    # Display total votes each candidate received
    print(f"{recorder.name_candidate_president_1} has {recorder.votes_candidate_president_1} votes; "
      f"{recorder.name_candidate_president_2} has {recorder.votes_candidate_president_2} votes;")
    print(f"{recorder.name_candidate_vice_president_1} has {recorder.votes_candidate_vice_president_1} votes; "
      f"{recorder.name_candidate_vice_president_2} has {recorder.votes_candidate_vice_president_2} votes;")

Call the main method

VoteRecorderDemo.main()

I feel like I’ve tried almost everything but I can’t seem to fix my output. I know its probably a stupid mistake or an easy fix but I am failing to see it.

New contributor

GrapesDuck is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

LEAVE A COMMENT