Question

In: Computer Science

The Electoral College plays a pivotal role in the election of the President of the US....

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

Solutions

Expert Solution

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:


Related Solutions

What role does the Electoral College play in the election of a president? Do you think the Electoral College should be modified or abolished? Why or why not?
EXERCISE 1 What role does the Electoral College play in the election of a president? Do you think the Electoral College should be modified or abolished? Why or why not? EXERCISE 2 What are some reasons why people might not vote? Have you personally encoun tered any of these reasons?
What is the Electoral College? How does the Electoral College work? Why is the Electoral College...
What is the Electoral College? How does the Electoral College work? Why is the Electoral College controversial? In your opinion, should the Electoral College by abolished, reformed, or kept as is? Why? Please be specific.
Americans do not have a constitutional right to choose their president. The Electoral College selects the...
Americans do not have a constitutional right to choose their president. The Electoral College selects the president, and, failing that, the House of Representatives selects the president. Still, presidents seek to act in the name of the American people. Increasingly throughout the twentieth century, presidents cultivated a popular role for themselves and reached out to the public. Is presidential rhetoric regarding popular mandates inconsistent with this fact of presidential selection? Should the Electoral College be reformed or abolished?
AMERICAN GOVERNMENT: Should the Electoral College continue to play a role in the selection of the...
AMERICAN GOVERNMENT: Should the Electoral College continue to play a role in the selection of the president? Do you think your vote counts? What's your take on negative campaigning?
What is the Electoral College? How does it work and what is its role in the...
What is the Electoral College? How does it work and what is its role in the election of the President?
How US President Election in 2020 impacts a global business?
How US President Election in 2020 impacts a global business?
What is the Electoral College? Do you feel that the Electoral College fits in a Democracy?...
What is the Electoral College? Do you feel that the Electoral College fits in a Democracy? Should the Electoral College be abolished? Why or why not?
Consider the following preferences and election problem. Let us assume that a president has to be...
Consider the following preferences and election problem. Let us assume that a president has to be elected. 4 candidates want to become a president, who are representing different political ideologies: A is a left-wing candidate, B is a social-democrat, C is a right-liberal candidate and D is a right-wing candidate. 20% of the voters (group left) preference A≻B≻C≻D, 30% of the voters (group social democrats) have the preference B≻A≻C≻D, 10% of the voters (group right-liberal) preference C≻B≻A≻D, 40% of the...
• Why is the Electoral College controversial? In three paragraphs, explain why opponents of the Electoral...
• Why is the Electoral College controversial? In three paragraphs, explain why opponents of the Electoral College object to it and what they argue should replace it (paragraph 1), why defenders of the Electoral College support its continued use (paragraph 2), and what you believe and why (paragraph 3).
Compare the electoral college to a popular vote approach for elections . •How does the electoral...
Compare the electoral college to a popular vote approach for elections . •How does the electoral college system operate/function? •What are consequences of using an electoral college system versus a popular vote? Use the 2000 presidential election as an example. 3. Assess the value of an individual citizen’s vote under the electoral college system. •Why does the U.S. still use the electoral college for presidential elections today?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT