Python名片管理系統 找了很久的 bug,最後發現是 input 沒轉換類型…… ...
Python名片管理系統
找了很久的 bug,最後發現是 input 沒轉換類型……
原始版:
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 print('*'*50)
5 print('名片管理系統')
6 print('*'*50)
7 print('''1. 新建名片
8 2. 刪除名片
9 3. 修改名片
10 4. 查詢名片
11 5. 顯示所有名片
12 6. 退出管理系統
13 ''')
14 List = []
15 while True:
16 choice = int(input('請選擇你的操作:'))#記住要轉換類型!
17 if choice == 1:
18 new_name = input('輸入姓名:')
19 new_sex = input('輸入性別:')
20 new_age = input('輸入年齡:')
21 new_QQ = input('輸入QQ號:')
22 card = {}
23 card['name'] =new_name
24 card['sex'] = new_sex
25 card['age'] = new_age
26 card['QQ'] = new_QQ
27 List.append(card)
28 print(List)
29
30 elif choice == 2:
31 fname = input('輸入你要刪除名片的姓名:')
32 fflag = 0#沒找到
33 for temp in List:
34 if temp['name'] == fname:
35 List.remove(temp)#此處宜用remove
36 print(List)
37 fflag = 1#找到了
38 break
39 if fflag == 0:
40 print('沒有這張名片!')
41
42 elif choice == 3:
43 mname = input('輸入要修改信息的名片的姓名:')
44 flag = 0
45 i = 0
46 for temp in List:
47 if mname == temp['name']:
48 flag = 1
49 print('%s\t%s\t%s\t%s'%(temp['name'], temp['sex'], temp['age'], temp['QQ']))
50 mchoice = int(input('選擇要修改的信息:1. 姓名 2. 性別 3. 年齡 4. QQ 5. 退出 :'))#記住轉換類型!!!
51 while True:
52 if mchoice == 1:
53 temp['name'] = input('輸入修改後姓名:')
55 elif mchoice == 2:
56 temp['sex'] = input('輸入修改後性別:')
57 elif mchoice == 3:
58 temp['age'] = input('輸入修改後年齡:')
59 elif mchoice == 4:
60 temp['QQ'] = input('輸入修改後QQ:')
61 elif mchoice == 5:
62 break
63 else:
64 print('輸入錯誤!')
65 print('姓名\t性別\t年齡\tQQ')
66 print('%s\t%s\t%s\t%s'%(temp['name'], temp['sex'], temp['age'], temp['QQ']))
67 break
68 if flag == 0:
69 print('無此名片!')
70
71 elif choice == 4:
72 fname = input('輸入你要查詢的名片中的姓名:')
73 fflag = 0#沒找到
74 for temp in List:
75 if temp['name'] == fname:
76 print('姓名\t性別\t年齡\tQQ')
77 print('%s\t%s\t%s\t%s'%(temp['name'], temp['sex'], temp['age'], temp['QQ']))
78 fflag = 1#找到了
79 break
80 if fflag == 0:
81 print('沒有這張名片!')
82 elif choice == 5:
83 print('姓名\t性別\t年齡\tQQ')
84 for temp in List:
85 print('%s\t%s\t%s\t%s'%(temp['name'], temp['sex'], temp['age'], temp['QQ']))
86 elif choice == 6:
87 break
88 else:
89 print('操作錯誤!請重新選擇!')
函數封裝版:
1 #!/usr/bin/env python3 2 # -*- coding: utf-8 -*- 3 4 List = [] 5 6 def print_menu(): 7 '''用此種註釋是可以輸入help(print_menu)查看到此註釋''' 8 print('*'*50) 9 print('名片管理系統') 10 print('*'*50) 11 print('1. 新建名片') 12 print('2. 刪除名片') 13 print('3. 修改名片') 14 print('4. 查詢名片') 15 print('5. 顯示所有名片') 16 print('6. 退出管理系統') 17 18 def m_add(): 19 new_name = input('輸入姓名:') 20 new_sex = input('輸入性別:') 21 new_age = input('輸入年齡:') 22 new_QQ = input('輸入QQ號:') 23 card = {} 24 card['name'] =new_name 25 card['sex'] = new_sex 26 card['age'] = new_age 27 card['QQ'] = new_QQ 28 global List#修改全局變數需要聲明 29 List.append(card) 30 print(List) 31 32 def m_del(): 33 global List 34 fname = input('輸入你要刪除名片的姓名:') 35 fflag = 0#沒找到 36 for temp in List: 37 if temp['name'] == fname: 38 List.remove(temp)#此處宜用remove 39 print(List) 40 fflag = 1#找到了 41 break 42 if fflag == 0: 43 print('沒有這張名片!') 44 45 def m_modify(): 46 global List 47 mname = input('輸入要修改信息的名片的姓名:') 48 flag = 0 49 i = 0 50 for temp in List: 51 if mname == temp['name']: 52 flag = 1 53 print('%s\t%s\t%s\t%s'%(temp['name'], temp['sex'], temp['age'], temp['QQ'])) 54 mchoice = int(input('選擇要修改的信息:1. 姓名 2. 性別 3. 年齡 4. QQ 5. 退出 :'))#記住轉化類型!!! 55 while True: 56 if mchoice == 1: 57 temp['name'] = input('輸入修改後姓名:') 58 flag = 1 59 elif mchoice == 2: 60 temp['sex'] = input('輸入修改後性別:') 61 elif mchoice == 3: 62 temp['age'] = input('輸入修改後年齡:') 63 elif mchoice == 4: 64 temp['QQ'] = input('輸入修改後QQ:') 65 elif mchoice == 5: 66 break 67 else: 68 print('輸入錯誤!') 69 print('姓名\t性別\t年齡\tQQ') 70 print('%s\t%s\t%s\t%s'%(temp['name'], temp['sex'], temp['age'], temp['QQ'])) 71 break 72 if flag == 0: 73 print('無此名片!') 74 75 def m_find(): 76 fname = input('輸入你要查詢的名片中的姓名:') 77 fflag = 0#沒找到 78 for temp in List: 79 if temp['name'] == fname: 80 print('姓名\t性別\t年齡\tQQ') 81 print('%s\t%s\t%s\t%s'%(temp['name'], temp['sex'], temp['age'], temp['QQ'])) 82 fflag = 1#找到了 83 break 84 if fflag == 0: 85 print('沒有這張名片!') 86 87 def m_showall(): 88 print('姓名\t性別\t年齡\tQQ') 89 for temp in List: 90 print('%s\t%s\t%s\t%s'%(temp['name'], temp['sex'], temp['age'], temp['QQ'])) 91 92 def main(): 93 print_menu() 94 while True: 95 choice = int(input('請選擇你的操作:'))#記住要轉化類型! 96 if choice == 1: 97 m_add() 98 99 elif choice == 2: 100 m_del() 101 102 elif choice == 3: 103 m_modify() 104 105 elif choice == 4: 106 m_find() 107 108 elif choice == 5: 109 m_showall() 110 111 elif choice == 6: 112 break 113 else: 114 print('操作錯誤!請重新選擇!') 115 116 117 main()
列表、字典作為全局變數可以不加 global