How to write a simple address book with Python
- 2021-11-24 02:18:57
- OfStack
Directory write a simple address book with Python 1. Idea 1, define an empty list and an empty dictionary to store
2. Define function options 3. Add address book function 4. Loop and call all functions 2. Demonstration of the whole project
3.4 Enquiry of Students
2. Define function options 3. Add address book function 4. Loop and call all functions 2. Demonstration of the whole project
Write a simple address book with Python
STEP 1 Conceive
1. Define an empty list and an empty dictionary to store
list1=[] # Used to store information in dictionaries
dict1={} # Used to store contact information
2. Define functional options
def Menu():
print(' Please select a function --------\n'
'1. Add Student \n'
'2. Delete Student \n'
'3. Modify students \n'
'4. Enquiry of Students \n'
'5. Show all students \n'
'6. Exit the system \n'
'----------------')
3. Add address book function 3.1 Adding Students
# Add Student
def Add():
id=input(' Please enter your student number :')
name=input(' Please enter a name :')
tel=input(' Please enter your mobile phone number :')
dict1={'id':id,'name':name,'tel':tel}
list1.append(dict1) # Add dictionary contents to the list
print(list1)
This completes the addition of students
3.2 Remove Student Feature
# Delete Student
def Del():
name=input(' Please enter the name of the student you want to delete :')
i=0 # Index
for item in list1: # Loop the contents of the list
i+=1
if name in item['name']: # When you determine that the name of the student to be deleted is the same as the nested dictionary name in the list
del list1[i-1] # Delete list index -1 Content of
print(list1)
else:
print(' There is no such student ...')
3.3 Modifying Students
# Modify students
def Update():
name=input(' Please enter the name of the student you want to modify :')
tel=input(' Please enter a new mobile phone number :')
i=0 # Index
for item in list1:
i+=1
if name in item['name']:
list1[i-1]['tel']=tel # Modify the list index -1 Dictionary contents nested in, old tel Modify to a new mobile phone number
print(list1)
else:
print(' There is no such student ...')
3.4 Enquiry of Students
# Enquiry of Students
def Select():
name=input(' Please enter the name of the student you want to query :')
for item in list1:
if name in item['name']:
print(' The student information found is as follows --------')
print(' The student's student number is %s, The name is %s, The cell phone number is %s' % (item['id'],item['name'],item['tel']))
else:
print(' No such person was found ...')
3.5 Show All Students
# Show all students
def ShowAll():
print(' Student number Name Mobile phone number ')
for item in list1:
print(item['id'],item['name'],item['tel'])
3.6 Exiting the system
# Exit the system
def Quit():
YesNo=input(' Are you sure you want to quit ,yes or no?')
if YesNo=='yes':
quit()
4. Loop and call all function functions
while True:
Menu()
num=int(input(' Please enter the function serial number :'))
if num==1:
Add()
elif num==2:
Del()
elif num==3:
Update()
elif num==4:
Select()
elif num==5:
ShowAll()
elif num==6:
Quit()
else:
print(' Invalid option -----')
2. Overall project demonstration
Please select a function --------
1. Add Student
2. Delete Student
3. Modify students
4. Enquiry of Students
5. Show all students
6. Exit the system
----------------
Please enter the function serial number :1
Please enter your student number :001
Please enter a name : Zhang
Please enter your mobile phone number :111
[{'id': '001', 'name': ' Zhang ', 'tel': '111'}]
Please select a function --------
1. Add Student
2. Delete Student
3. Modify students
4. Enquiry of Students
5. Show all students
6. Exit the system
----------------
Please enter the function serial number :1
Please enter your student number :002
Please enter a name : Li
Please enter your mobile phone number :222
[{'id': '001', 'name': ' Zhang ', 'tel': '111'}, {'id': '002', 'name': ' Li ', 'tel': '222'}]
Please select a function --------
1. Add Student
2. Delete Student
3. Modify students
4. Enquiry of Students
5. Show all students
6. Exit the system
----------------
Please enter the function serial number :1
Please enter your student number :003
Please enter a name : Wang
Please enter your mobile phone number :333
[{'id': '001', 'name': ' Zhang ', 'tel': '111'}, {'id': '002', 'name': ' Li ', 'tel': '222'}, {'id': '003', 'name': ' Wang ', 'tel': '333'}]
Please select a function --------
1. Add Student
2. Delete Student
3. Modify students
4. Enquiry of Students
5. Show all students
6. Exit the system
----------------
Please enter the function serial number :2
Please enter the name of the student you want to delete : Zhang
[{'id': '002', 'name': ' Li ', 'tel': '222'}, {'id': '003', 'name': ' Wang ', 'tel': '333'}]
There is no such student ...
Please select a function --------
1. Add Student
2. Delete Student
3. Modify students
4. Enquiry of Students
5. Show all students
6. Exit the system
----------------
Please enter the function serial number :3
Please enter the name of the student you want to modify : Wang
Please enter a new mobile phone number :666
There is no such student ...
[{'id': '002', 'name': ' Li ', 'tel': '222'}, {'id': '003', 'name': ' Wang ', 'tel': '666'}]
Please select a function --------
1. Add Student
2. Delete Student
3. Modify students
4. Enquiry of Students
5. Show all students
6. Exit the system
----------------
Please enter the function serial number :4
Please enter the name of the student you want to query : Wang
No such person was found ...
The student information found is as follows --------
The student's student number is 003, The name is Wang , The cell phone number is 666
Please select a function --------
1. Add Student
2. Delete Student
3. Modify students
4. Enquiry of Students
5. Show all students
6. Exit the system
----------------
Please enter the function serial number :5
Student number Name Mobile phone number
002 Li 222
003 Wang 666
Please select a function --------
1. Add Student
2. Delete Student
3. Modify students
4. Enquiry of Students
5. Show all students
6. Exit the system
----------------
Please enter the function serial number :6
Are you sure you want to quit ,yes or no?yes
Process finished with exit code 0