-
Notifications
You must be signed in to change notification settings - Fork 11
/
sd39.py
80 lines (69 loc) · 2.08 KB
/
sd39.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# create a mapping of provinces to abbreviation
provinces = {
'Alberta': 'AB',
'British Columbia': 'BC',
'Manitoba': 'MB',
'New Brunswick': 'NB',
'Newfoundland and Labrador': 'NL',
'Nova Scotia': 'NS',
'Northwest Territories': 'NT',
'Nunavut': 'NU',
'Ontario': 'ON',
'Prince Edward Island': 'PE',
'Quebec': 'QC',
'Saskatchewan': 'SK',
'Yukon': 'YT'
}
# create a basic set of states and some cities in them
cities = {
'AB': 'Edmonton',
'BC': 'Victoria',
'MB': 'Winnipeg',
'NB': 'Frederiction',
'NL': 'St. John\'s',
'NS': 'Halifax',
'NT': 'Yellowknife',
'NU': 'Iqaluit',
'ON': 'Toronto',
'PE': 'Charlottetown'
}
# add some more cities
cities['QC'] = 'Quebec City'
cities['SK'] = 'Regina'
cities['YT'] = 'Whitehorse'
# print out some cities
print '-' * 10
print "ON Province has: ", cities['ON']
print "BC Province has: ", cities['BC']
print "QC Province has: ", cities['QC']
# print out some provinces
print '-' * 10
print "British Columbia's abbreviation is: ", provinces['British Columbia']
print "Nova Scotia's abbreviation is: ", provinces['Nova Scotia']
print "Prince Edward Island's abbreviation is: ", provinces['Prince Edward Island']
# do it by using the province then cities dict
print '-' * 10
print "Ontario has: ", cities[provinces["Ontario"]]
print "Quebec has: ", cities[provinces["Quebec"]]
print "Alberta has: ", cities[provinces["Alberta"]]
# print every province abbreviation
print '-' * 10
for province, abbrev in provinces.items():
print "%s is abbreviated %s" % (province, abbrev)
# print every city in province
print '-' * 10
for abbrev, city in cities.items():
print "%s has the city %s" % (abbrev, city)
# now do both at the same time
print '-' * 10
for province, abbrev in provinces.items():
print "%s province is abbreviated %s and has city %s" % (
province, abbrev, cities[abbrev])
print '-' * 10
# safely get a abbreviation by province that might not be there
province = provinces.get('Texas')
if not province:
print "Sorry, no Texas."
# get a city with a default value
city = cities.get('TX', 'Does Not Exist')
print "The city for the state 'TX' is: %s" % city