In: Computer Science
The Electoral College plays a pivotal role in the election of the President of the US.
The following is a dictionary of the states, the number of electoral votes for each state and the winner of the state. The key is the name of state, the value is a list containing the number of votes for that stare and the projected winner ('U' for Unknown).
Write a program that will present to the user the name of each state, gather from the user who you think will win the electoral votes for that state and record the winner in the list ('B' for Biden, 'T' for Trump, ‘U’ for Undecided/Too Close to Call).
Output the states won by each candidate and undecided (sorted alphabetically), the total electoral votes for each candidate and the winner of the election (270 of the possible 538 votes are needed to win).
Here is a dictionary of the states and their respective electoral votes, please copy and past this code to your file
ec = {'Alabama': [9,'U'],
'Kentucky' : [8,'U'],
'North Dakota' : [3,'U'],
'Alaska' : [3,'U'],
'Louisiana' : [8,'U'],
'Ohio' : [18,'U'],
'Arizona' : [11,'U'],
'Maine' : [4,'U'],
'Oklahoma' : [7,'U'],
'Arkansas' : [6,'U'],
'Maryland' : [10,'U'],
'Oregon' : [7,'U'],
'California' : [55,'U'],
'Massachusetts' : [11,'U'],
'Pennsylvania' : [20,'U'],
'Colorado': [9,'U'],
'Michigan' : [16,'U'],
'Rhode Island' : [4,'U'],
'Connecticut' : [7,'U'],
'Minnesota' : [10,'U'],
'South Carolina' : [9,'U'],
'Delaware' : [3,'U'],
'Mississippi' : [6,'U'],
'South Dakota' : [3,'U'],
'District of Columbia' : [3,'U'],
'Missouri' : [10,'U'],
'Tennessee' : [11,'U'],
'Florida' : [29,'U'],
'Montana' : [3,'U'],
'Texas' : [38,'U'],
'Georgia' : [16,'U'],
'Nebraska' : [5,'U'],
'Utah' : [6,'U'],
'Hawaii' : [4,'U'],
'Nevada' : [6,'U'],
'Vermont' : [3,'U'],
'Idaho' : [4,'U'],
'New Hampshire' : [4,'U'],
'Virginia' : [13,'U'],
'Illinois' : [20,'U'],
'New Jersey' : [14,'U'],
'Washington' : [12,'U'],
'Indiana' : [11,'U'],
'New Mexico' : [5,'U'],
'West Virginia' : [5,'U'],
'Iowa' : [6,'U'],
'New York' : [29,'U'],
'Wisconsin' : [10,'U'],
'Kansas' : [6,'U'],
'North Carolina' : [15,'U'],
'Wyoming' : [3,'U'],
}
In Python format Please
Code:
import pandas as pd
ec = {'Alabama': [9,'U'],
'Kentucky' : [8,'U'],
'North Dakota' : [3,'U'],
'Alaska' : [3,'U'],
'Louisiana' : [8,'U'],
'Ohio' : [18,'U'],
'Arizona' : [11,'U'],
'Maine' : [4,'U'],
'Oklahoma' : [7,'U'],
'Arkansas' : [6,'U'],
'Maryland' : [10,'U'],
'Oregon' : [7,'U'],
'California' : [55,'U'],
'Massachusetts' : [11,'U'],
'Pennsylvania' : [20,'U'],
'Colorado': [9,'U'],
'Michigan' : [16,'U'],
'Rhode Island' : [4,'U'],
'Connecticut' : [7,'U'],
'Minnesota' : [10,'U'],
'South Carolina' : [9,'U'],
'Delaware' : [3,'U'],
'Mississippi' : [6,'U'],
'South Dakota' : [3,'U'],
'District of Columbia' : [3,'U'],
'Missouri' : [10,'U'],
'Tennessee' : [11,'U'],
'Florida' : [29,'U'],
'Montana' : [3,'U'],
'Texas' : [38,'U'],
'Georgia' : [16,'U'],
'Nebraska' : [5,'U'],
'Utah' : [6,'U'],
'Hawaii' : [4,'U'],
'Nevada' : [6,'U'],
'Vermont' : [3,'U'],
'Idaho' : [4,'U'],
'New Hampshire' : [4,'U'],
'Virginia' : [13,'U'],
'Illinois' : [20,'U'],
'New Jersey' : [14,'U'],
'Washington' : [12,'U'],
'Indiana' : [11,'U'],
'New Mexico' : [5,'U'],
'West Virginia' : [5,'U'],
'Iowa' : [6,'U'],
'New York' : [29,'U'],
'Wisconsin' : [10,'U'],
'Kansas' : [6,'U'],
'North Carolina' : [15,'U'],
'Wyoming' : [3,'U'],
} # given dictionary of states
ec_dataframe = pd.DataFrame(ec) # Creating dataframe
# Initializing Lists and variables
biden= []
trump = []
undec = []
vote_b = 0
vote_t = 0
vote_u = 0
for k in ec_dataframe.columns: # Iterating through columns
cand = input(f'Who do you think will win in {k} (B/T/U): ') # User input of who will win the state
ec_dataframe[k][1] = cand # Update dataframe with the winner of the state
if cand == 'B':
biden = biden + [k] # Add to list of states won by Biden
vote_b = vote_b + ec_dataframe[k][0] # Add number of votes won by Biden
elif cand == 'T':
trump = trump + [k] # Add to list of states won by Trump
vote_t = vote_t + ec_dataframe[k][0] # Add number of votes won by Trump
elif cand == 'U':
undec = undec + [k] # Add to list of states undecided
vote_u = vote_u + ec_dataframe[k][0] # Add number of votes undecided
biden.sort() # Sort states alphabetically
print(f'States won by Biden: {biden}')
trump.sort() # Sort states alphabetically
print(f'States won by Trump: {trump}')
undec.sort() # Sort states alphabetically
print(f'States still undecided/too close to call: {undec}')
print(f'Electrol votes won by Biden: {vote_b}')
print(f'Electrol votes won by Biden: {vote_t}')
print(f'Electrol Votes undecided: {vote_u}')
# To check who won the election
if vote_b >= 270:
print('Biden has won the Election!!!')
elif vote_t >= 270:
print('Trump has won the Election!!!')
else:
print('Election result is still undecided')
Output: