score = int(input("请输入成绩:")) if score >= 90 and score <= 100: print(str(score) + "分是优秀!") elif score >= 80 and score <= 89: print(str(score) + "分是良好!") elif score >= 70 and score <= 79: print(str(score) + "分是中等!") elif score >= 60 and score <= 69: print(str(score) + "分是及格!") elif score < 60 and score >= 0: print("很遗憾!" + str(score) + "分不及格!") else: print("输入成绩不在0到100之间!")
时间: 2024-03-15 14:41:30 浏览: 29
这是一个用 Python 编写的成绩评定程序,根据输入的分数判断其对应的等级。如果输入的分数在 90 到 100 之间,则输出“分是优秀!”;如果在 80 到 89 之间,则输出“分是良好!”;如果在 70 到 79 之间,则输出“分是中等!”;如果在 60 到 69 之间,则输出“分是及格!”;如果小于 60 分且大于等于 0 分,则输出“很遗憾!分不及格!”;如果输入成绩不在 0 到 100 之间,则输出“输入成绩不在0到100之间!”。
相关问题
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()` 方法来遍历。
- 对于计算平均成绩,应该在遍历时进行,而不是在输入时计算。
if a==2: id2 = input("请输入要修改的学生学号:") modfile_file1 = open("C:\\Users\\ASUS\\Desktop\\student.txt","r",encoding="UTF-8") # 以可读方式打开文件,读取到line_info中,每一行就是一个列表的元素 line_info = modfile_file1.readlines() modfile_file2 = open("C:\\Users\\ASUS\\Desktop\\student.txt","w",encoding="UTF-8") # 用以写入修改后的数据 modfile_file2.close() for line in line_info: # 遍历列表 if id2 in line: # 如果修改的学生信息存在,就重新写入学生信息 print("已找到,请修改信息!") name = input("请输入姓名:") score_chi = input("请输入语文成绩:") score_math = input("请输入数学成绩:") score_eng = input("请输入英语成绩:") score_py = input("请输入python成绩:") sum_score = int(scor_chi)+int(score_eng) + int(score_py) + int(score_math) #stu_info = id2 + '\t' + name + '\t' + score_eng + '\t' + score_py + '\t' + \mod_score_math + '\t' + str(mod_sum_score) + '\n' modfile_file2.write(sum_score) print("修改成功!!!") continue modfile_file2.write(line) 怎么修改这段代码
这段代码有一些问题,需要进行修改。具体来说:
1. 在打开文件 `modfile_file2` 后,应该先写入标题行,否则修改后的文件将缺少标题行。
2. 在修改学生信息时,需要将输入的成绩转换为整型后再进行加和操作。
3. 在写入修改后的学生信息时,需要将学生信息拼接成一个字符串后再写入文件,否则会报错。
下面是修改后的代码:
```
if a == 2:
id2 = input("请输入要修改的学生学号:")
modfile_file1 = open("C:\\Users\\ASUS\\Desktop\\student.txt", "r", encoding="UTF-8")
line_info = modfile_file1.readlines()
modfile_file1.close()
modfile_file2 = open("C:\\Users\\ASUS\\Desktop\\student.txt", "w", encoding="UTF-8")
modfile_file2.write("学号\t姓名\t语文\t数学\t英语\tPython\t总分\n") # 写入标题行
for line in line_info:
if id2 in line:
print("已找到,请修改信息!")
name = input("请输入姓名:")
score_chi = input("请输入语文成绩:")
score_math = input("请输入数学成绩:")
score_eng = input("请输入英语成绩:")
score_py = input("请输入python成绩:")
sum_score = int(score_chi) + int(score_eng) + int(score_py) + int(score_math)
stu_info = id2 + '\t' + name + '\t' + score_chi + '\t' + score_math + '\t' + score_eng + '\t' + score_py + '\t' + str(sum_score) + '\n'
modfile_file2.write(stu_info)
print("修改成功!!!")
else:
modfile_file2.write(line)
modfile_file2.close()
```