提示:文章寫完後,目錄可以自動生成,如何生成可參考右邊的幫助文檔
項目需求
主要實作功能:對聯系人資訊的添加、删除、檢視、修改
提示:以下是本篇文章正文内容,下面案例可供參考,本文主要供新手學習項目,難度不大,僅供參考。
一、使用步驟
1.使用清單
很多人使用字典儲存,但字典對于新手一上手會很繁瑣,是以這裡使用清單來儲存資料
# 姓名和聯系方式
phone_toxl = []
phone_lxr = []
2.添加
name、num、email、address儲存資料,随後将資料填入phone_lxr清單中
# 添加
def insert():
name = input('請輸入聯系人姓名:')
num = input('請輸入聯系方式:')
email = input('請輸入郵箱位址:')
address = input('請輸入家庭住址:')
phone_lxr = [name, num, email, address]
phone_toxl.append(phone_lxr)
3.檢視
很簡單的循環周遊二維數組檢視資料
# 檢視
def select():
leng = len(phone_toxl)
for i in range(0, leng):
print("***************************")
print(f"聯系人姓名:{phone_toxl[i][0]}")
print(f"聯系方式:{phone_toxl[i][1]}")
print(f"郵箱位址:{phone_toxl[i][2]}")
print(f"家庭住址:{phone_toxl[i][3]}")
4.删除
使用循環的周遊,查找聯系人姓名,進行修改
def delete():
xz = input('請輸入要删除的聯系人姓名:')
f=True
for i in range(0, len(phone_toxl)):
if phone_toxl[i][0] == xz:
phone_toxl.pop(i)
f=True
break
else:f=False
if f:
print('删除成功!')
else:
print('查無此人')
5.修改
将查詢和添加融合在一起,制作修改
def update():
xz = input('請輸入要修改的聯系人姓名:')
num = input('請輸入修改聯系方式:')
email = input('請輸入修改郵箱位址:')
address = input('請輸入修改家庭住址:')
a=[xz,num,email,address]
f = True
for i in range(0, len(phone_toxl)):
if phone_toxl[i][0] == xz:
phone_toxl[i]=a
f=True
break
else:
f=False
if f:print('修改成功!')
else:print('查無此人')
2.總體代碼
代碼如下(示例):
# 姓名和聯系方式
phone_toxl = []
phone_lxr = []
# 添加
def insert():
name = input('請輸入聯系人姓名:')
num = input('請輸入聯系方式:')
email = input('請輸入郵箱位址:')
address = input('請輸入家庭住址:')
phone_lxr = [name, num, email, address]
phone_toxl.append(phone_lxr)
# 檢視
def select():
leng = len(phone_toxl)
for i in range(0, leng):
print("***************************")
print(f"聯系人姓名:{phone_toxl[i][0]}")
print(f"聯系方式:{phone_toxl[i][1]}")
print(f"郵箱位址:{phone_toxl[i][2]}")
print(f"家庭住址:{phone_toxl[i][3]}")
def delete():
xz = input('請輸入要删除的聯系人姓名:')
f=True
for i in range(0, len(phone_toxl)):
if phone_toxl[i][0] == xz:
phone_toxl.pop(i)
f=True
break
else:f=False
if f:
print('删除成功!')
else:
print('查無此人')
def update():
xz = input('請輸入要修改的聯系人姓名:')
num = input('請輸入修改聯系方式:')
email = input('請輸入修改郵箱位址:')
address = input('請輸入修改家庭住址:')
a=[xz,num,email,address]
f = True
for i in range(0, len(phone_toxl)):
if phone_toxl[i][0] == xz:
phone_toxl[i]=a
f=True
break
else:
f=False
if f:print('修改成功!')
else:print('查無此人')
# 運作
print('************** 1.添加聯系人、2.檢視聯系人、3.删除聯系人、4.修改資訊、0.退出 ******************')
while True:
inp = int(input('功能選擇:'))
if inp == 1:
insert()
elif inp == 2:
select()
elif inp == 3:
delete()
elif inp == 4:
update()
else:
break
總結
這隻是一個課上小練習,本人也是剛剛接觸python,想對剛學的小白們提供一點幫助。
寫的不是很好,有問題希望指出