-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_area_code.py
More file actions
76 lines (58 loc) · 2.56 KB
/
Copy pathadd_area_code.py
File metadata and controls
76 lines (58 loc) · 2.56 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
def main():
try:
# Open the file phone_numbers.txt for reading and read all
# of the phone numbers into a list named phone_numbers.
phone_numbers = read_list("phone_numbers.txt")
# Print the list of phone numbers which will show that
# some of the phone number don't have an area code.
print(phone_numbers)
# Some of the phone numbers in phone_numbers.txt do not start
# with an area code. Replace each short phone number with a
# phone number that begins with the area code "208-" To do this,
# call the map function and pass the add_area_code function and
# the list of phone numbers as arguments to the map function.
pass
# Print the list with the corrected phone numbers.
print(new_numbers)
except (FileNotFoundError, PermissionError) as error:
print(type(error).__name__, error, sep=": ")
def add_area_code(phone_number):
"""Phone numbers in the U.S. are often stored as ten digits and
two dashes in this format: "ddd-ddd-dddd" where each d is a digit.
If the length of phone_number is less than 12 characters, add the
area code "208-" at the beginning of phone_number and return
phone_number.
Parameter phone_number: a string of digits formatted as
"ddd-dddd" or "ddd-ddd-dddd"
Return: a string of digits formated as "ddd-ddd-dddd"
"""
pass
def read_list(filename):
"""Read the contents of a text file into a list and
return the list. Each element in the list will contain
one line of text from the text file.
Parameter filename: the name of the text file to read
Return: a list of strings
"""
# Create an empty list named text_list.
text_list = []
# Open the text file for reading and store a reference
# to the opened file in a variable named text_file.
with open(filename, "rt") as text_file:
# Read the contents of the text
# file one line at a time.
for line in text_file:
# Remove white space, if there is any,
# from the beginning and end of the line.
clean_line = line.strip()
# Append the clean line of text
# onto the end of the list.
text_list.append(clean_line)
# Return the list that contains the lines of text.
return text_list
# If this file is executed like this:
# > python add_area_code.py
# then call the main function. However, if this file is simply
# imported (e.g. into a test file), then skip the call to main.
if __name__ == "__main__":
main()