Skip to content

Instantly share code, notes, and snippets.

@beiweiqiang
Last active July 28, 2018 01:36
Show Gist options
  • Save beiweiqiang/c19c26a6aa9d11884823e2a2618be7bf to your computer and use it in GitHub Desktop.
Save beiweiqiang/c19c26a6aa9d11884823e2a2618be7bf to your computer and use it in GitHub Desktop.
python practice 01
# 用于添加, 浏览, 删除 联系人的电话
import pickle
import os
data = []
target = 'backup'
file_name = 'backup_data.data'
path = os.path.join(target, file_name)
if os.path.exists(path):
with open(path, 'rb') as f:
data = pickle.load(f)
# 添加联系人
def add():
print('add new person:')
name = input('input name ->')
phone = int(input('input phone ->'))
data.append({ 'name': name, 'phone': phone })
print('add new person success')
print('')
# 删除联系人
def delete():
name = input('who do you want to delete ->')
for item in data:
if item['name'] == name:
data.remove(item)
print('remove person successful')
print('')
# 枚举联系人
def list_all():
for item in data:
print(item)
print('')
def exit_backup():
with open(path, 'wb') as f:
pickle.dump(data, f)
print('backup success')
print('')
while True:
print('what op do you want to do ?')
print('a: add new person')
print('d: delete a person')
print('l: list all person')
print('e: exit and your data will be backup')
op = input('your input ->')
print('')
if op == 'a':
add()
elif op == 'd':
delete()
elif op == 'l':
list_all()
elif op == 'e':
exit_backup()
break
else:
print('re input')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment