-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStateChecker.py
66 lines (52 loc) · 2.59 KB
/
StateChecker.py
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
########################################################################################################################
# MISSION STATEMENT #
########################################################################################################################
# This code is used for the development of the Income Tax Calculator. It checks the user's input for the standard two-
# letter abbrevation of a state's name. I aim to make this take as few lines of code as necessary by creating an dataset
# in the form of either a list or a dictionary so that the program can automatically match up any two standard abbrevi-
# ation to its matching full name.
#
# Author: Xander Leatherwood
#
# Contact: [email protected]
########################################################################################################################
import csv
from collections import defaultdict
# This section opens the State_Rates csv file in order to create a list of states (full names).
columns = defaultdict(list)
with open("State_Rates.csv", encoding="Latin-1") as f:
reader = csv.DictReader(f)
for row in reader:
for (k, v) in row.items():
columns[k].append(v)
f.close()
states = columns['State']
try:
while True:
states.remove("")
except ValueError:
pass
# Create a dictionary that assigns abbreviations to each entry in the list of states.
state_abbs = ['AL','AK','AZ','AR','CA','CO','CT','DE','FL','GA','HI','ID','IL','IN','IA','KS','KY','LA','ME','MD','MA','MI','MN','MS',
'MO','MT','NE','NV','NH','NJ','NM','NY','NC','ND',"OH",'OK','OR','PA','RI','SC','SD','TN','TX','UT','VT','VA','WA','WV','WI','WY','DC']
state_dict = {}
# Only items with key-value pairs ("X":"Y") can be added to dictionaries. Therefore, a string must be created that takes the name of the
# state, its abbreviation, and then combines them into a key-value pair.
for s in states:
state_index = states.index(s)
state_abb = state_abbs[state_index]
state_dict.update({state_abb: s})
state_dict_keys = state_dict.keys()
state_check = None
while state_check is None:
try:
state_input = input("Enter your state (full name or two-letter postal abbreviation): ")
if state_input in states:
my_state = state_input
break
elif state_input in state_dict_keys:
my_state = state_dict[state_input]
break
except ValueError:
print("Invalid input!")
print(my_state)