forked from inglesp/python-data-structure-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathradio_freq.py
More file actions
85 lines (72 loc) · 2.2 KB
/
radio_freq.py
File metadata and controls
85 lines (72 loc) · 2.2 KB
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
81
82
83
84
85
# This program knows about the frequencies of various FM radio stations in
# London.
#
# Usage:
#
# $ python radio_freq.py [station_name]
#
# For instance:
#
# $ python radio_freq.py "Radio 4"
# You can listen to Radio 4 on 92.5 FM
#
# or:
#
# $ python radio_freq.py "BBC Radio 5"
# I don't know the frequency of BBC Radio 5
import argparse
fm_frequencies = {
"89.1 MHz": "BBC Radio 2",
"91.3 MHz": "BBC Radio 3",
"93.5 MHz": "BBC Radio 4",
"94.9 MHz": "BBC London",
"95.8 MHz": "Capital FM",
"97.3 MHz": "LBC",
"98.8 MHz": "BBC Radio 1",
"100.0 MHz": "Kiss FM",
"100.9 MHz": "Classic FM",
"105.4 MHz": "Magic",
"105.8 MHz": "Virgin",
"106.2 MHz": "Heart 106.2",
}
def main():
arg_dict = get_args()
radio_station = arg_dict["radio_station"]
if not radio_station:
print_table_of_available_stations()
return
for k, v in fm_frequencies.items():
if v.lower() == radio_station.lower():
print(f"You can listen to {radio_station} on {k[:-4]} FM")
return
print(f"I don't know the frequency of {radio_station}. Here are the stations I do know about:")
list_stations()
def get_args():
parser = argparse.ArgumentParser(
prog="radio_freq",
description="Display FM frequencies of known radio stations.",
)
parser.add_argument(
"radio_station",
help="the name of the radio station",
nargs="?",
default="",
type=str,
)
return vars(parser.parse_args())
def list_stations():
for v in fm_frequencies.values():
print(f"{v}")
def print_table_of_available_stations():
print(f"{"Frequencies":<11} | {"Station"}\n--------------------------")
for k, v in fm_frequencies.items():
print(f"{k:<11} | {v}")
if __name__ == "__main__":
main()
# TODO:
# * Implement the program as described in the comments at the top of the file.
# TODO (extra):
# * Change the program so that if the radio station is not found, the user is
# given a list of all stations that the program does know about.
# * Change the program so that if it is called without arguments, a table of
# all radio stations and their frequencies is displayed.