-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex39.1.py
67 lines (54 loc) · 1.69 KB
/
ex39.1.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
# -*- coding: utf-8 -*-
from sys import argv
script, filename = argv
# create a mapping of province to abbreviation
provinces = {
'Mazowieckie': 'MA',
'Łódzkie': 'LO',
'Małopolskie': 'MP',
'Pomorskie': 'PM',
'Śląskie': 'SL'
}
# create a basic set of province and some cities in them
cities = {
'MA': 'Warszawa',
'LO': 'Łódź',
'SL': 'Katowice'
}
# add some more cities
cities['PM'] = 'Gdańsk'
cities['MP'] = 'Kraków'
# print out more cities
print '-' * 10
print "MA province has: ", cities['MA']
print "SL province has: ", cities['SL']
# print some province
print '-' * 10
print "Mazowieckie's abbreviation is: ", provinces['Mazowieckie']
print "Małopolskie's abbreviation is: ", provinces['Małopolskie']
# do it by using the province then cities dict
print '-' * 10
print "Mazowieckie has: ", cities[provinces['Mazowieckie']]
print "Małopolskie has: ", cities[provinces['Małopolskie']]
# print every province abbreviation
with open(filename, 'w') as f:
f.write ('-' * 10)
for province, abbrev in provinces.items():
f.write ("%s is abbreviated %s " % (province, abbrev))
f.close()
# 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 state that might not be there
province = provinces.get('Lubelskie', None)
if not province:
print "Sorry, no Lubelskie."
# get a city with a default value
city = cities.get('LU', 'Does Not Exist')
print "The city for province 'LU' is: %s" % city