2018/9/22 My_Phone_Directory
http://localhost:8888/nbconvert/html/Desktop/Python/My_Phone_Directory.ipynb?download=false 1/4
<font color = 'blue'>Case Study: Making a Phone Directory
1. We build a system where we ask the user to
a. enter record
b. Do a query
c. Load a file
d Save a file
e Exit
2. In our case enter a record means entering a name and a telephone number
3. So our system is simple. We just want a mechanism of storing names and phone numbers
4. Any given a name we want to pull out a phone number
1. There are many problems which have this format.
2. A menu with a list of possible actions that can be taken
3. A mechanism for retrieving data from a file and storing new data that you have
4. We have different routines for the different actions.
5. We have to be very careful on how we select data structures for the different activities
a. In practice the input and output routines have to be carefully designed so as to be idiot proof.
b. We do not want bad data to enter the system.
c. For example we will add some minimal features to ensure the telephone number is entered in a
format that we can use.
d. We may also want to provide some form of assistance to the query function. For example as the
person types a name the computer gives suggestions from the data base.
1. So we have a main program with perhaps 4 functions
2. We probably need a menu function as well
3. What data structures do we need.
2018/9/22 My_Phone_Directory
http://localhost:8888/nbconvert/html/Desktop/Python/My_Phone_Directory.ipynb?download=false 2/4
In [1]:
def main():
commands = [(1, 'Enter Records'), (2, 'Query'),
(3, 'Load File'), (4, 'Save'), (5, 'Exit')]
myInd = True
while myInd:
for i, s in commands:
print(i, s)
a = input()
if a == "":
break
a = int(a)
if a == 1:
pass
add_entry()
if a == 2:
pass
query()
if a == 3:
pass
loadfile()
if a == 4:
pass
savefile()
if a== 5:
myInd = False
phone={}
main()
1 Enter Records
2 Query
3 Load File
4 Save
5 Exit
5
2018/9/22 My_Phone_Directory
http://localhost:8888/nbconvert/html/Desktop/Python/My_Phone_Directory.ipynb?download=false 3/4
In [3]:
def add_entry():
a = 'Enter Name: '
a = input(a)
a = a.strip() # Just in case there are spaces at front or back
b = 'Enter Tel. Number'
b = input(b)
b = b.strip() # We want to get rid of leading and ending blank spaces
mylist = b.split() # create a list of numbers ( in case spaces are encountered in the mid
dle)
tel = ''.join(mylist) #concatenates the list
#Put into dictionary
phone[a] = tel
print(' The name ',a,' and tel: ', tel, ' has been added')
return
add_entry()
#ans,tel =add_entry()
#print(ans, tel)
In [4]:
def query():
a = 'Enter Name'
a = input(a)
a = a.strip()
b = phone.get(a)
if b ==None:
print("The name is not available")
else:
print('The Telephone Number of ', a, ' is ', b)
print('')
print('---------------------')
print('')
query()
Enter Name: Peter
Enter Tel. Number 216 368 3849
The name Peter and tel: 2163683849 has been added
Enter NamePeter
The Telephone Number of Peter is 2163683849
---------------------
2018/9/22 My_Phone_Directory
http://localhost:8888/nbconvert/html/Desktop/Python/My_Phone_Directory.ipynb?download=false 4/4
In [5]:
# Store each
def savefile():
a = input('Enter File Name to save to')
fn = open(a, 'w')
for k in phone: # recall k is the keyword ( name) and phone[k] is the phone number
fn.write(k+'\n')
fn.write(phone[k]+'\n')
fn.close()
savefile() # The file consists of alternate lines of names and phone numbers
In [6]:
def loadfile():
phone = {} # create a dictionary called phone
fname = input('Enter file to load: ')
try:
myfile = open(fname, 'r')
mylist = myfile.readlines() # reads all lines in the file into a list each line
#is an element in the list. It adds\n
for i in range(0, len(mylist), 2):
key_str = mylist[i].strip('\n')
val_str = mylist[i + 1].strip('\n')
phone[key_str] = val_str
print(fname, 'successfully loaded.')
myfile.close()
return phone
except:
print('I could not find the file')
return phone
phone = loadfile()
Enter File Name to save tomyfile
Enter file to load: myfile
myfile successfully loaded.
版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。