初学Python,只能一点点练习和应用,看似很多循环有点晕。
特别是在“改信息”那一块循环,大家有好的写法,请多指教,感谢!
[Python] 纯文本查看 复制代码 students = []
print('-' * 30)
print('欢迎使用简易学生通讯录系统 V0.1')
print('1 - 增加学生信息')
print('2 - 删除学生信息')
print('3 - 修改学生信息')
print('4 - 查询学生信息')
print('5 - 退出系统')
print('-' * 30)
while True:
user_num = input('请输入操作编号:')
if user_num.isdigit():
user_num = int(user_num)
if user_num == 1:
student = {}
student['name'] = input('请录入学生姓名:')
student['age'] = int(input('请录入学生年龄:'))
student['mobile'] = input('请录入学生电话:')
students.append(student)
print(students)
elif user_num == 2:
name = input('请输入要删除学生姓名:')
for i in students:
if i['name'] == name:
students.remove(i)
print(f'{name}已删除成功!')
break
if i['name'] != name:
print(f'{name}不在通讯录中!')
print(students)
elif user_num == 3:
name = input('请输入要修改学生姓名:')
for i in students:
flag = 0
if i['name'] == name:
newName = input('请输入新姓名:')
i['name'] = newName
newAge = input('请输入新年龄:')
i['age'] = newAge
newMobile = input('请输入新电话:')
i['mobile'] = newMobile
print(f'{name}所有信息已修改成功!')
flag = 1
break
if flag == 0:
print(f'{name}不在通讯录中!')
print(students)
elif user_num == 4:
name = input('请输入要查询学生姓名:')
for i in students:
if i['name'] == name:
print(i)
break
if i['name'] != name:
print(f'{name}不在通讯录中!')
elif user_num == 5:
print('已退出系统!')
break
else:
print('编号输入有误,请重新输入!')
else:
print(f'{user_num}不是正确的数字编号') |