-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyPoll_starter.py
More file actions
70 lines (35 loc) · 1.68 KB
/
PyPoll_starter.py
File metadata and controls
70 lines (35 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# -*- coding: UTF-8 -*-
"""PyPoll Homework Starter File."""
# Import necessary modules
import csv
import os
# Files to load and output (update with correct file paths)
file_to_load = os.path.join("Resources", "election_data.csv") # Input file path
file_to_output = os.path.join("analysis", "election_analysis.txt") # Output file path
# Initialize variables to track the election data
total_votes = 0 # Track the total number of votes cast
# Define lists and dictionaries to track candidate names and vote counts
# Winning Candidate and Winning Count Tracker
# Open the CSV file and process it
with open(file_to_load) as election_data:
reader = csv.reader(election_data)
# Skip the header row
header = next(reader)
# Loop through each row of the dataset and process it
for row in reader:
# Print a loading indicator (for large datasets)
print(". ", end="")
# Increment the total vote count for each row
# Get the candidate's name from the row
# If the candidate is not already in the candidate list, add them
# Add a vote to the candidate's count
# Open a text file to save the output
with open(file_to_output, "w") as txt_file:
# Print the total vote count (to terminal)
# Write the total vote count to the text file
# Loop through the candidates to determine vote percentages and identify the winner
# Get the vote count and calculate the percentage
# Update the winning candidate if this one has more votes
# Print and save each candidate's vote count and percentage
# Generate and print the winning candidate summary
# Save the winning candidate summary to the text file