-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphone_numbers.py
84 lines (75 loc) · 2.25 KB
/
phone_numbers.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
81
82
83
84
# Phone numbers
# Pulled from https://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python_3/File_IO
# on November 1, 2018.
def print_numbers(numbers):
print("Telephone Numbers:")
for k, v in numbers.items():
print("Name:", k, "\tNumber:", v)
print()
def add_number(numbers, name, number):
numbers[name] = number
def lookup_number(numbers, name):
if name in numbers:
return "The number is " + numbers[name]
else:
return name + " was not found"
def remove_number(numbers, name):
if name in numbers:
del numbers[name]
else:
print(name," was not found")
def load_numbers(numbers, filename):
in_file = open(filename, "rt")
while True:
in_line = in_file.readline()
if not in_line:
break
in_line = in_line[:-1]
name, number = in_line.split(",")
numbers[name] = number
in_file.close()
def save_numbers(numbers, filename):
out_file = open(filename, "wt")
for k, v in numbers.items():
out_file.write(k + "," + v + "\n")
out_file.close()
def print_menu():
print('1. Print Phone Numbers')
print('2. Add a Phone Number')
print('3. Remove a Phone Number')
print('4. Lookup a Phone Number')
print('5. Load numbers')
print('6. Save numbers')
print('7. Quit')
print()
phone_list = {}
menu_choice = 0
print_menu()
while True:
menu_choice = int(input("Type in a number (1-7): "))
if menu_choice == 1:
print_numbers(phone_list)
elif menu_choice == 2:
print("Add Name and Number")
name = input("Name: ")
phone = input("Number: ")
add_number(phone_list, name, phone)
elif menu_choice == 3:
print("Remove Name and Number")
name = input("Name: ")
remove_number(phone_list, name)
elif menu_choice == 4:
print("Lookup Number")
name = input("Name: ")
print(lookup_number(phone_list, name))
elif menu_choice == 5:
filename = input("Filename to load: ")
load_numbers(phone_list, filename)
elif menu_choice == 6:
filename = input("Filename to save: ")
save_numbers(phone_list, filename)
elif menu_choice == 7:
break
else:
print_menu()
print("Goodbye")