|
| 1 | +# Thanks to Stiched aka discord user id 220689361627250688 for implementing a lot of the code to this project as well as additional discord members for their help and support. |
| 2 | +# Initialize vote counts and electoral votes |
| 3 | +total_votes = 0 |
| 4 | +candidate_1_electoral_votes = 0 |
| 5 | +candidate_2_electoral_votes = 0 |
| 6 | +candidate_1_popular_votes = 0 # New variable for Candidate 1 popular votes |
| 7 | +candidate_2_popular_votes = 0 # New variable for Candidate 2 popular votes |
| 8 | + |
| 9 | +# Electoral Locations and their Electoral Votes |
| 10 | +electoral_votes_locations = { |
| 11 | + "Alabama": 9, |
| 12 | + "Alaska": 3, |
| 13 | + "Arizona": 11, |
| 14 | + "Arkansas": 6, |
| 15 | + "California": 54, # Adjusted for 2020 Census |
| 16 | + "Colorado": 10, |
| 17 | + "Connecticut": 7, |
| 18 | + "Delaware": 3, |
| 19 | + "District of Columbia": 3, # Washington, D.C. |
| 20 | + "Florida": 30, |
| 21 | + "Georgia": 16, |
| 22 | + "Hawaii": 4, |
| 23 | + "Idaho": 4, |
| 24 | + "Illinois": 19, |
| 25 | + "Indiana": 11, |
| 26 | + "Iowa": 6, |
| 27 | + "Kansas": 6, |
| 28 | + "Kentucky": 8, |
| 29 | + "Louisiana": 8, |
| 30 | + "Maine": 4, |
| 31 | + "Maryland": 10, |
| 32 | + "Massachusetts": 11, |
| 33 | + "Michigan": 15, |
| 34 | + "Minnesota": 10, |
| 35 | + "Mississippi": 6, |
| 36 | + "Missouri": 10, |
| 37 | + "Montana": 3, |
| 38 | + "Nebraska": 5, |
| 39 | + "Nevada": 6, |
| 40 | + "New Hampshire": 4, |
| 41 | + "New Jersey": 14, |
| 42 | + "New Mexico": 5, |
| 43 | + "New York": 28, |
| 44 | + "North Carolina": 16, |
| 45 | + "North Dakota": 3, |
| 46 | + "Ohio": 17, |
| 47 | + "Oklahoma": 7, |
| 48 | + "Oregon": 7, |
| 49 | + "Pennsylvania": 19, |
| 50 | + "Rhode Island": 4, |
| 51 | + "South Carolina": 9, |
| 52 | + "South Dakota": 3, |
| 53 | + "Tennessee": 11, |
| 54 | + "Texas": 40, |
| 55 | + "Utah": 6, |
| 56 | + "Vermont": 3, |
| 57 | + "Virginia": 13, |
| 58 | + "Washington": 12, |
| 59 | + "West Virginia": 4, |
| 60 | + "Wisconsin": 10, |
| 61 | + "Wyoming": 3, |
| 62 | +} |
| 63 | + |
| 64 | +counted_locations = {} |
| 65 | + |
| 66 | +def choose_vote_location(candidate_1_electoral_votes, candidate_2_electoral_votes): |
| 67 | + location = input("\nSelect your location (or type 'end' to end election): ").strip() |
| 68 | + |
| 69 | + if location in electoral_votes_locations: |
| 70 | + print(location) |
| 71 | + state_voting(location, candidate_1_electoral_votes, candidate_2_electoral_votes) |
| 72 | + elif location != "end" and location not in electoral_votes_locations: |
| 73 | + print("Invalid State Chosen") |
| 74 | + elif location == "end": |
| 75 | + end_election() |
| 76 | + |
| 77 | + return location |
| 78 | + |
| 79 | +def state_voting(location, candidate_1_electoral_votes, candidate_2_electoral_votes): |
| 80 | + global candidate_1_popular_votes, candidate_2_popular_votes # Access global variables |
| 81 | + |
| 82 | + if location in counted_locations: |
| 83 | + print(f"{location} has already closed their polling places and decided their votes.") |
| 84 | + else: |
| 85 | + candidate_1_state_votes = 0 |
| 86 | + candidate_2_state_votes = 0 |
| 87 | + total_state_votes = 0 |
| 88 | + winner = "" |
| 89 | + |
| 90 | + while True: |
| 91 | + vote = input( |
| 92 | + f"Vote for Candidate 1 ('1') or Candidate 2 ('2') or close ('close') the polls. " |
| 93 | + ) |
| 94 | + |
| 95 | + if vote not in ["1", "2", "CLOSE", "close"]: |
| 96 | + print("Invalid selection") |
| 97 | + else: |
| 98 | + if vote == "1": |
| 99 | + candidate_1_state_votes += 1 |
| 100 | + candidate_1_popular_votes += 1 # Increment popular votes |
| 101 | + total_state_votes += 1 |
| 102 | + continue |
| 103 | + elif vote == "2": |
| 104 | + candidate_2_state_votes += 1 |
| 105 | + candidate_2_popular_votes += 1 # Increment popular votes |
| 106 | + total_state_votes += 1 |
| 107 | + continue |
| 108 | + elif vote in ["CLOSE", "close"]: |
| 109 | + print(f"-" * 60) |
| 110 | + print(f"The total votes cast in {location} is {total_state_votes}") |
| 111 | + print(f"-" * 60) |
| 112 | + print(f"Candidate 1 had a total of {candidate_1_state_votes} votes") |
| 113 | + print(f"Candidate 2 had a total of {candidate_2_state_votes} votes") |
| 114 | + print(f"-" * 60) |
| 115 | + |
| 116 | + if candidate_1_state_votes > candidate_2_state_votes: |
| 117 | + winner = "Candidate 1" |
| 118 | + candidate_1_electoral_votes += electoral_votes_locations[location] |
| 119 | + if candidate_2_state_votes > candidate_1_state_votes: |
| 120 | + winner = "Candidate 2" |
| 121 | + candidate_2_electoral_votes += electoral_votes_locations[location] |
| 122 | + |
| 123 | + print( |
| 124 | + f"The winner of {location} is {winner} gaining {electoral_votes_locations[location]} electoral votes" |
| 125 | + ) |
| 126 | + |
| 127 | + counted_locations[location] = [ |
| 128 | + total_state_votes, |
| 129 | + candidate_1_electoral_votes, |
| 130 | + candidate_2_electoral_votes, |
| 131 | + ] |
| 132 | + |
| 133 | + break |
| 134 | + |
| 135 | +def end_election(): |
| 136 | + sum_votes = 0 |
| 137 | + candidate_1_sum = 0 |
| 138 | + candidate_2_sum = 0 |
| 139 | + |
| 140 | + # total up all votes cast for entire country |
| 141 | + for k in counted_locations.keys(): |
| 142 | + sum_votes += counted_locations[k][0] |
| 143 | + |
| 144 | + # total up ALL candidate 1 electoral votes from counted dictionary |
| 145 | + for k in counted_locations.keys(): |
| 146 | + candidate_1_sum += counted_locations[k][1] |
| 147 | + |
| 148 | + # total up ALL candidate 2 electoral votes from counted dictionary |
| 149 | + for k in counted_locations.keys(): |
| 150 | + candidate_2_sum += counted_locations[k][2] |
| 151 | + |
| 152 | + print(f"-" * 60) |
| 153 | + print(f"All of the polls in the United States are closed.") |
| 154 | + print(f"-" * 60) |
| 155 | + print(f"Total votes cast across the country : {sum_votes}") |
| 156 | + |
| 157 | + print(f"-" * 60) |
| 158 | + print(f"Candidate 1 - Total Electoral Votes : {candidate_1_sum}") |
| 159 | + print(f"Candidate 1 - Total Popular Votes : {candidate_1_popular_votes}") # Display popular votes |
| 160 | + print(f"-" * 60) |
| 161 | + print(f"Candidate 2 - Total Electoral Votes : {candidate_2_sum}") |
| 162 | + print(f"Candidate 2 - Total Popular Votes : {candidate_2_popular_votes}") # Display popular votes |
| 163 | + print(f"-" * 60) |
| 164 | + |
| 165 | + if candidate_1_sum > candidate_2_sum: |
| 166 | + print(f"Candidate 1 becomes the new President of the United States!") |
| 167 | + if candidate_2_sum > candidate_1_sum: |
| 168 | + print(f"Candidate 2 becomes the new President of the United States!") |
| 169 | + print(f"-" * 60) |
| 170 | + |
| 171 | + raise SystemExit |
| 172 | + |
| 173 | +while True: |
| 174 | + choose_vote_location(candidate_1_electoral_votes, candidate_2_electoral_votes) |
0 commit comments