-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarket directory.py
57 lines (51 loc) · 1.91 KB
/
market directory.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
"""
#aishamalik
#am5080
"""
from collections import defaultdict
fmarket = defaultdict(list)
towntozip = defaultdict(set)
def read_markets(filename):
"""
Read in the farmers market data from the file named filename and return
a tuple of two objects:
1) A dictionary mapping zip codes to lists of farmers market tuples.
2) A dictionary mapping towns to sets of zip codes.
"""
f = open(filename,'r')
for line in f:
line = tuple(line.split('#'))
zipcodes = line[4]
fmname = (line[1], line[2], line [3], line[0], zipcodes)
town = line[3].lower()
fmarket[zipcodes].append(fmname)
towntozip[town].add(zipcodes)
return fmarket, towntozip
def print_market(market):
"""
Returns a human-readable string representing the farmers market tuple
passed to the market parameter.
"""
final = '{} \n{} \n{} {} {}'.format(market[0], market[1], market[2], market[3], market[4])
return final
if __name__ == "__main__":
try:
read_markets('markets.txt')
user = input("Enter a zipcode, town name or quit. ")
while user != 'quit':
if user.isdigit():
if user in fmarket:
for mkts in fmarket[user]:
print(print_market(mkts))
else:
print("No market found :(")
else:
if user in towntozip:
for town in towntozip[user]:
for zippy in towntozip[user]:
print(print_market(zippy))
else:
print("No market found :(")
user = input("Enter a zipcode, town name or quit. ")
except (FileNotFoundError, IOError):
print("Error reading {}".format("market.txt"))