使用while循环结构实现:连续随机(input函数)输入4名学生的成绩,求出4名学生的平均成绩(保留2位小数,本知识点请百度)。
时间: 2024-05-19 10:17:21 浏览: 7
您好!这是一个关于Python编程的问题,我很乐意帮您回答。
以下是Python代码,可以实现您提供的要求:
total = 0 # 初始化学生成绩总和为0
count = 0 # 初始化学生人数为0
while count < 4:
score = input("请输入学生成绩:") # 使用input函数随机输入成绩
total += float(score) # 将输入的成绩转换成浮点数并累加到总成绩中
count += 1 # 计数器加1
average = total / 4 # 计算平均成绩
print("4名学生的平均成绩为:{:.2f}".format(average)) # 输出结果
请注意:这只是它的一个例子,可能有其他更优雅的解决方案。
相关问题
编写input()函数完成学生数据记录的输入与输出,要求记录条数不小于5,每个学生的信息包括学号,姓名和三门课程的成绩。要求使用list来模拟学生记录结构,请完善下面的程序。
```python
# 定义一个空列表来存储学生记录
students = []
# 获取学生记录条数
num = int(input("请输入学生记录条数(不少于5条):"))
# 判断输入的记录条数是否满足要求
while num < 5:
print("记录条数不能少于5条,请重新输入")
num = int(input("请输入学生记录条数:"))
# 循环获取每个学生的信息并添加到列表中
for i in range(num):
print("请输入第%d个学生的信息:" % (i+1))
stu_id = input("学号:")
name = input("姓名:")
grade1 = float(input("第一门课程成绩:"))
grade2 = float(input("第二门课程成绩:"))
grade3 = float(input("第三门课程成绩:"))
# 将学生信息添加到列表中
students.append([stu_id, name, grade1, grade2, grade3])
# 打印学生记录
print("学生记录如下:")
print("学号\t\t姓名\t\t成绩1\t成绩2\t成绩3")
for s in students:
print("%s\t%s\t%.2f\t%.2f\t%.2f" % (s[0], s[1], s[2], s[3], s[4]))
```
def insert(): # 录入学生信息 student_list = [] while True: id = input('请输入学生学号:') if not id: break name = input('请输入学生姓名:') if not name: break try: english = int(input('请输入学生英语成绩:')) python = int(input('请输入学生python成绩:')) math = int(input('请输入学生数学成绩:')) data = int(input('请输入学生数据结构成绩:')) except: print('成绩录入有错,请检查后重新录入!') continue stu = {'id': id, 'name': name, 'english': english, 'python': python, 'math': math, 'data': data} student_list.append(stu) a = input('\n是否继续录入?y/n:') if a == 'y' or a == 'Y': # 当输入y时,执行下一轮循环,接着录入,否则终止循环 continue else: break save(student_list) # 向文件中写入学生成绩数据 print('\n学生成绩录入成功!') 写出上面这段代码的伪代码
1. 定义函数insert()
2. 初始化空列表student_list
3. 进入循环
4. 输入学生学号id
5. 如果id为空,跳出循环
6. 输入学生姓名name
7. 如果name为空,跳出循环
8. 尝试输入学生英语成绩english、python成绩python、数学成绩math、数据结构成绩data
9. 如果输入有误,输出提示信息,并返回第3步
10. 创建字典stu,包含学生学号、姓名、英语成绩、python成绩、数学成绩、数据结构成绩
11. 将字典stu添加到student_list列表中
12. 询问是否继续录入,如果是,返回第3步,否则跳出循环
13. 调用save()函数,将学生成绩数据写入文件
14. 输出学生成绩录入成功信息
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)