Question

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 file should be named 'REPORT-PresidentialElections.txt', but if the input file is named 'MidTermElections.txt', then the output file should be named 'REPORT-MidTermElections.txt', etc. Your program should work with any properly formatted data file -- not just the two listed here.
    • The report should include one line for each year in the data file. The line should show a sentence containing the year, the percentage of eligible voters who registered, and the percentage of eligible voters who cast ballots (voted). The percentages should be rounded to two places after the decimal point. Here is an example of what the first part of the output file would look like for the data shown above:
In 1952, 90.81% registered and 72.80% voted.
In 1956, 89.45% registered and 71.75% voted.
...
  • Finally, show the following information in the Python shell window in a nicely-formatted, easy to understand report:
    • the total number of years listed
    • the total number of ballots cast in all those years
    • the average percentage of eligible voters who registered
    • the number of years with less than 60% of registered voters casting ballots
    • the percentage of years with more than 80% of registered voters casting ballots
    • A sentence showing the name of the report file

Here is an example of what the report would look like for the first file:

The total number of years listed: 17

Total ballots cast in all these years: 34,436,792

Average percentage of eligible voters registered: 79.55%

Number of years with less than 60% of registered voters casting ballots: 0

Percentage of years with more than 80% of registered voters casting ballots: 47.1%

An output file named REPORT-PresidentialElections.txt has been created.

Presidential Election txt file as follows:

1952 1533500 1392594 1116414 1956 1622500 1451375 1164104 1960 1753700 1527510 1257952 1964 1857900 1582046 1276956 1968 1975000 1649734 1310942 1972 2306000 1974849 1519771 1976 2546000 2065378 1584590 1980 2992000 2236603 1722904 1984 3182000 2457667 1931546 1988 3417000 2499309 1923043 1992 3818000 2814680 2324907 1996 4122000 3078208 2293895 2000 4368000 3335714 2517028 2004 4646000 3508208 2884783 2008 5010844 3630118 3071587 2012 5221125 3904959 3172930 2016 5557921 4270270 3363440

Mid Term election txt as follows:

1958 1703200 1375035 978400 1962 1813500 1446593 971706 1966 1869400 1472054 987134 1970 2078000 1562916 1123000 1974 2419000 1896214 1044425 1978 2651000 1960900 1028854 1982 3119000 2105563 1404831 1986 3307000 2230354 1358160 1990 3650000 2225101 1362651 1994 4000000 2896519 1733471 1998 4257000 3119562 1939421 2002 4519000 3209648 1808720 2006 4821000 3264511 2107370 2010 5149729 3601268 2565589 2014 5376986 3922248 2124330

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

CODE:

infile = input('Enter input file name : ')

fh = open(infile,'r')

outfile = "REPORT-"+infile

fh2 = open(outfile,'w')

year_list,total_list,register_list,voted_list=[],[],[],[]

for line in fh.readlines():
    year,total,register,voted = list(map(int,line.split()))
    year_list.append(year)
    total_list.append(total)
    register_list.append(register)
    voted_list.append(voted)
    
print()  
print('The total numbers of years listed:',len(year_list))
print()
print('Total ballots cast in all these years:',sum(voted_list))
print()
eligible = sum(voted_list)/sum(register_list)*100
print('Average percentage of eligible voters registered:',round(eligible,2),'%')
print()
year60 = 0
year80 = 0

for i in range(len(year_list)):
    reg_per = round(register_list[i]/total_list[i]*100,2)
    vote_per = round(voted_list[i]/total_list[i]*100,2)
    eligible = voted_list[i]/register_list[i]*100
    if eligible < 60: year60 += 1
    if eligible > 80: year80 += 1
    fh2.write('In '+str(year_list[i])+' , '+str(reg_per)+' % registered and '+str(vote_per)+' % voted.\n')
    
print('Number of years with less than 60% of registered voters casting ballots:',year60)
print()
print('Percentage of years with more than 80% of registered voters casting ballots:',round(year80/len(year_list)*100,1),'%')
print()
print('An output file named REPORT-PresidentialElections.txt has been created.')
fh.close()
fh2.close()

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
It's Python :D Write a program that does the following: Allow the user to specify 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 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...

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

  • Write a PYTHON program that asks the user for the name of the file. The program...

    Write a PYTHON program that asks the user for the name of the file. The program should write the contents of this input file to an output file. In the output file, each line should be preceded with a line number followed by a colon. The output file will have the same name as the input filename, preceded by “ln” (for linenumbers). Be sure to use Try/except to catch all exceptions. For example, when prompted, if the user specifies “sampleprogram.py”...

  • Write a python program that prompts the user for the names of two text files and...

    Write a python program that prompts the user for the names of two text files and compare the contents of the two files to see if they are the same. If they are, the scripts should simply output “Yes”. If they are not, the program should output “No”, followed by the first lines of each file that differ from each other. The input loop should read and compare lines from each file. The loop should break as soon as a...

  • Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The progra...

    Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs opening any of the 3 For example, (user input shown in caps in first line) Enter first...

  • Exercise 1 — Input information then write to a file Write an application that will have...

    Exercise 1 — Input information then write to a file Write an application that will have someone input any number of friends' phone data and write the data to a file. File Details The output file should be named "friend1.txt" The data in the files will be last name, first name, age and phone number, each piece of data on a separate line, such as: Smith Pete 15 888-1234 Jones Sue 17 885-4578 Use parallel arrays to create this information...

  • 4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java...

    4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java source code file named H01_43.java (your main class is named H01_43). Problem: Write a program that prompts the user for the name of a Java source code file (you may assume the file contains Java source code and has a .java filename extension; we will not test your program on non-Java source code files). The program shall read the source code file and output...

  • This assignment tests your ability to write C programs that handle keyboard input, formatted console output,...

    This assignment tests your ability to write C programs that handle keyboard input, formatted console output, and input/output with text files. The program also requires that you use ctype functions to deal with the logic. In this program, you will input from the user two characters, which should both be letters where the second letter > the first letter. You will then input a file character-by-character and output those letters that fall between the two characters (inclusively) to an output...

  • C++ (1) Write a program to prompt the user for an input and output file name....

    C++ (1) Write a program to prompt the user for an input and output file name. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs. For example, (user input shown in caps in first line, and in second case, trying to write to a folder which you may not have write authority in) Enter input filename: DOESNOTEXIST.T Error opening input file: DOESNOTEXIST.T...

  • Write a C program to compute average grades for a course. The course records are in...

    Write a C program to compute average grades for a course. The course records are in a single file and are organized according to the following format: Each line contains a student’s first name, then one space, then the student’s last name, then one space, then some number of quiz scores that, if they exist, are separated by one space. Each student will have zero to ten scores, and each score is an integer not greater than 100. Your program...

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
ADVERTISEMENT