-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
98 lines (83 loc) · 3.62 KB
/
main.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# -*- coding: UTF-8 -*-
# “你帮我助”物资交换软件v1.0
# @Time : 2022.10.5.
# @Author : 徐可懿
# 打印每行时通用的格式化
fmt = "{0:{4}<6}\t{1:{4}<6}\t{2:{4}<6}\t{3:<10}"
def print_list():
"""一个辅助函数,可以将整个物品清单对齐打印,更加美观"""
print(fmt.format("物品名称", "物品数量", "联系人姓名", "联系电话", chr(12288)))
print('-----------------------------------------------')
with open("goods_data.txt", encoding='utf-8') as data:
for line in data:
print(fmt.format(line.split()[0], line.split()[1], line.split()[2], line.split()[3], chr(12288)))
def add():
"""用于在平台上添加物品信息"""
print('请根据提示输入需要添加的物品信息')
goods = input('物品名称: ')
quantity = input('物品数量: ')
owner = input('联系人姓名: ')
tele = input('联系电话: ')
with open("goods_data.txt", mode='a', encoding='utf-8') as data:
data.write(goods + '\t' + quantity + '\t' + owner + '\t' + tele + '\n')
def delete():
"""根据物品名称+联系电话删除物品信息"""
print('请根据提示依次输入要删除的物品名称,持有者联系电话')
goods = input('物品名称: ')
tele = input('联系电话: ')
is_found = False
# 将文件逐行读取并存于lines中,以便找到对应信息后将整行删去
with open("goods_data.txt", "r", encoding="utf-8") as f:
lines = f.readlines()
# print(lines)
with open("goods_data.txt", "w", encoding="utf-8") as f_w:
for line in lines:
if not is_found and line.split()[0] == goods and line.split()[3] == tele:
is_found = True
continue
f_w.write(line)
if is_found:
print('已删除')
else:
print('未找到对应信息,无法删除')
def search():
"""根据物品名称查找物品"""
is_found = False
goods = input('请输入您想要寻找的物品名称: ')
with open("goods_data.txt", encoding='utf-8') as data:
for line in data:
if line.split()[0] == goods:
is_found = True
print(fmt.format(line.split()[0], line.split()[1], line.split()[2], line.split()[3], chr(12288)))
if not is_found:
print('未找到对应信息')
if __name__ == '__main__':
print('欢迎使用“你帮我助”v1.0\n')
help = '使用说明:\n' \
'输入“Add”--在平台上添加物品信息\n' \
'输入“Delete”--根据物品名称+联系电话删除物品信息\n' \
'输入“Print”--打印在列表中的物品信息\n' \
'输入“Search”--根据物品名称查找物品\n' \
'输入“Help”--打印帮助\n' \
'输入“Quit”--结束程序\n'
print(help)
prompt = '\n请输入您希望进行的操作(Quit可终止程序):'
choice = ''
while choice != 'Quit':
choice = input(prompt)
choice = choice.replace(" ", "")
choice = choice.capitalize()
if choice == 'Add':
add()
elif choice == 'Delete':
delete()
elif choice == 'Print':
print_list()
elif choice == 'Search':
search()
elif choice == 'Help':
print(help)
elif choice == 'Quit':
continue
else:
print('您的输入不合规范(输入“Help”获取帮助),请检查后重新输入,谢谢!')