Question

Write a Python program to solve the following problem: Information is available about the latest municipal election. For...

Write a Python program to solve the following problem:

Information is available about the latest municipal election. For each voter, the following data is given:

  • voter id number
  • age of voter
  • the ward number (1, 2, 3 or 4) in which the voter resides
  • the name of the person the voter selected for mayor

Input this information and compute and output the following statistics:

  • the total number of voters
  • the number of people under the age of 25 who voted
  • the number of people from Ward 1 who voted for John Smith
  • the number of people in Ward 2 who voted
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the complete code as per the requirements. It is well explained inside the code using comments.

# total voters
totalVoters = 0
# under 25 voters
under25 = 0
# number of people from Ward 1 who voted for John Smith
ward1JS = 0
# number of people in Ward 2 who voted
ward2 = 0

# read data until user enter 0
while(True):
    print()
    voterID = input('Enter the voter ID number [0 to stop]: ')

    # if user enterd 0, stop reading data
    if(voterID == '0'):
        break
  
    age = int(input('Age of voter: '))
    wardNo = int(input('Ward number [1,2,3,4]: '))
    votedFor = input('Name of person the voter selected for mayor: ');

    # calculate the stats
    totalVoters += 1

    # if voter under 25
    if(age < 25):
        under25 += 1

    # if from Ward 1 and voted for John Smith
    if(wardNo == 1 and votedFor == 'John Smith'):
        ward1JS += 1

    # if from ward 2
    if(wardNo == 2):
        ward2 += 1

# output the satistics
print()
print('Total number of voters:', totalVoters)
print('Number of people under the age of 25 who voted:', under25)
print('Number of people from Ward 1 who voted for John Smith:', ward1JS)
print('Number of people in Ward 2 who voted:',ward2)

Below is the screenshot of code in case there is any issue with indentation:

total voters totalVoters 0 # under 25 voters under25 = 0 #number of people from Ward who voted for John Smith wardlJS=0 numbe

output the satistics print print Total number of voters:, totalVoters) print Number of people under the age of 25 who voted

Below is the sample output:

stop] 587 Enter the voter ID number [0 to Age of voter: 24 Ward number [1,2,3,4]: 1 Name of person the voter selected for may

This completes the requirement. Let me know if you have any queries.

Thanks!

Add a comment
Know the answer?
Add Answer to:
Write a Python program to solve the following problem: Information is available about the latest municipal election. For...
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 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...

  • It's Python :D Write a program that does the following: Allow the user to specify the...

    It's Python :D Write a program that does the following: Allow the user to specify the name of an input file containing voter participation data (that way, different data files can be tested). Read the data from the file and write a report out to a new file as follows: The name of the output file should be the name of the input file preceded with the word 'REPORT-'. So, if the input file is named 'PresidentialElections.txt', then the output...

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

  • Python code Write a Python program for each of the following: The following is a social...

    Python code Write a Python program for each of the following: The following is a social network graph. Each edge (link) in the graph represents a friendship between the involved people. The number on the link represents the number of times the involved people communicated with one another. No number on an edge means 0 communication is happening. You are to write a python program that will store the relations between the people involved in this social network somehow, as...

  • python question 2. In a file called passfail.py, write a Python program to solve the following...

    python question 2. In a file called passfail.py, write a Python program to solve the following problem: Given a file students.txt with the following information for a stu dent: student number, average quiz mark, average assignment mark, midterm exam mark, and final exam mark (where each mark is recorded out of 100), de termine if the student has passed or failed based on the following information: • A student will pass the course if they have a passing mark (50%...

  • 2. In a file called passfail.py, write a Python program to solve the following problem: Given...

    2. In a file called passfail.py, write a Python program to solve the following problem: Given a file students.txt with the following information for a stu dent: student number, average quiz mark, average assignment mark, midterm exam mark, and final exam mark (where each mark is recorded out of 100), de termine if the student has passed or failed based on the following information A student will pass the course if they have a passing mark (50% or higher) for...

  • Create the Python code for a program adhering to the following specifications. Write an Employee class...

    Create the Python code for a program adhering to the following specifications. Write an Employee class that keeps data attributes for the following pieces of information: - Employee Name (a string) - Employee Number (a string) Make sure to create all the accessor, mutator, and __str__ methods for the object. Next, write a class named ProductionWorker that is a subclass of the Employee class. The ProductionWorker class should keep data attributes for the following information: - Shift number (an integer,...

  • Write, test and document a Python program, in a file called MarsCoins.py, to solve the following problem Marvin from Ma...

    Write, test and document a Python program, in a file called MarsCoins.py, to solve the following problem Marvin from Mars has a jar full of martian currency (Maruvians, Caruvians, Taruvians and Paruvians). The breakdown of the currency units on Mars is as follows: The single smallest unit of currency is a Paruvian 6 Paruvians 1 Taruvian 12 Paruvians 1 Caruvian 24 Paruvians1 Maruvian Marvin wants to share his money with his friends but he doesn.t want to carry around all...

  • Write a program in Python to solve the following task: In this project, you will build...

    Write a program in Python to solve the following task: In this project, you will build a phone number from digits entered by your user. Your program will loop until 10 valid digits have been entered. It will then use those digits to display the phone number entered using the format: XXXXXXXXXX (or (XXX) XXX – XXXX for extra credit). The program will ask for a digit, it will check to see if the digit is actually between 0 and...

  • Write a program that does the following in Python Code: Stores the following three lists: last_name...

    Write a program that does the following in Python Code: Stores the following three lists: last_name = ["Smith", "Jones", "Williams", "Bailey", "Rogers", "Pyle", "Rossington"] first_name =["Lisa", "Bill", "Jay", "Sally", "Walter","Jake","Gary"] phone_number =["240-233-1921", "301-394-2745", "571-321-8934", "703-238-3432", "703-947-9987", "301-945-3593", "240-671-3221"] Has a function named combine_lists() that takes in last_names, first_names and phone_numbers as lists and returns a dictionary called phone_book that uses last_name as the key and a list consisting of first_name and phone_number. Has a main function that iterates through the...

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