while True: yg_id=input('请输入要删除的员工的id: ') if yg_id!='': if os.path.exists(filename): with open(filename, 'r' , encoding='utf-8')as file: yg_old=file.readlines() else: yg_old=[] flag=False if stydent_old: with open(filename,'w',encoding='utf-8') as wfile: d={} for item in yg_old: with open(filename,'w', encoding= 'utf-8') as wfile: d={} for item in yg_old: d=hict(eval(item)) if d['id']!=yg_id: wfile.write(str(d)+'\n') else: flag=True if flag: print(f"id为{yg_id}的员工信息已被删除") else: print(f"没有找到id为{yg_id}的员工信息") else: print("无员工信息") break show() answer=input('是否继续删除?y\n/n') if answer=='y': continye else: break
时间: 2024-02-10 10:34:50 浏览: 32
这段代码可能存在一些缩进错误,但大致的功能是:不断循环,让用户输入要删除的员工的id,如果输入不为空,则检查是否存在文件,如果存在,则读取文件中所有员工信息;否则,将已有员工信息设置为空列表。然后,遍历员工信息列表,将每个员工信息转换为字典,检查字典中的id是否与用户输入的id相同,如果相同,则将该员工信息从文件中删除,否则将该员工信息写回文件。最后,展示所有员工信息,并询问用户是否继续删除。如果用户输入'y',则继续循环;否则,跳出循环。
相关问题
帮我优化下列代码import time user = 'cxk' # 用户名 password = '1314' # 登录密码 a = 0 while True: user_input = input('请输入用户名:') user_password = input('请输入密码:') if user_input != user and user_password != password: print('密码或用户名错误') continue else: if user_input ==user and user_password == password: print('登录成功') continue money=5000000 name=input("请输入姓名:")
这段代码可以进行如下优化:
1. 使用函数封装登录逻辑,提高代码可复用性。
2. 使用异常处理机制来避免输入非法数据引起的程序异常。
3. 将变量名改为更具有可读性的名称。
4. 在代码中添加注释,方便自己和他人理解代码意图。
下面是优化后的代码:
```python
def login():
"""登录函数"""
username = 'cxk'
password = '1314'
count = 0 # 记录输入密码错误的次数
while count < 3:
try:
user_input = input('请输入用户名:')
user_password = input('请输入密码:')
if user_input == username and user_password == password:
print('登录成功')
return True
else:
count += 1
print('密码或用户名错误,请重新输入!')
except Exception as e:
print('输入非法数据!请重新输入!')
print('输入密码错误次数过多,程序退出!')
return False
if login():
money = 5000000
name = input("请输入姓名:")
```
优化后的代码中,将原始的 while True 循环改为了 login 函数,该函数返回 True 表示登录成功,返回 False 表示登录失败。对于输入非法数据,使用异常处理机制进行处理,防止程序异常退出。同时,变量名改为了更具有可读性的名称,代码中添加了注释,代码更加易读易懂。
score_dict = {}while True: input_str = input().strip() if input_str == '#': break name, score = input_str.split() score = int(score) if name in score_dict: score_dict[name].append(score) else: score_dict[name] = [score]print('姓 名 平均成绩')for name, scores in score_dict: avg_score = sum(scores) / len(scores) print('{:<8} {:.2f}'.format(name, avg_score))
这段代码存在一些问题,应该修改为:
```python
score_dict = {}
while True:
input_str = input().strip()
if input_str == '#':
break
name, score = input_str.split()
score = int(score)
if name in score_dict:
score_dict[name].append(score)
else:
score_dict[name] = [score]
print('姓 名 平均成绩')
for name, scores in score_dict.items():
avg_score = sum(scores) / len(scores)
print('{:<8} {:.2f}'.format(name, avg_score))
```
主要修改包括:
- 对于 `for name, scores in score_dict`,应该改为 `for name, scores in score_dict.items()`,因为字典类型需要使用 `items()` 方法来遍历。
- 对于计算平均成绩,应该在遍历时进行,而不是在输入时计算。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)