用python输入学号查询成绩并计算排名的代码
时间: 2024-12-10 12:27:33 浏览: 19
在Python中,你可以创建一个简单的字典来模拟成绩数据,并编写一个函数来接受学号作为输入,查询对应的成绩并进行排名。下面是一个简化的示例:
```python
# 定义学生的成绩字典,键是学号,值是成绩
scores = {
'001': 90,
'002': 85,
'003': 95,
'004': 88,
'005': 92,
# 更多学生...
}
def get_score_and_rank(student_id):
if student_id in scores:
score = scores[student_id]
# 对所有学生的成绩进行排序,得到排名
all_scores = sorted(scores.values())
rank = len([s for s in all_scores if s >= score]) + 1
return f"学号 {student_id} 的成绩是 {score},排名是 {rank} 位"
else:
return "该学号不存在"
# 示例用法
print(get_score_and_rank('003'))
```
相关问题
用python运用panda的read_excel函数读取xls文件,并使用字典存储学生成绩数据,输入学号查询成绩并计算排名的代码
在Python中,我们可以使用pandas库中的`read_excel`函数来读取.xls文件,然后将数据转换成字典,方便后续查找和处理。以下是一个简单的示例:
```python
import pandas as pd
# 读取Excel文件
df = pd.read_excel('your_file.xls') # 将'your_file.xls'替换为实际的文件路径
# 将DataFrame转换为字典,键是学号,值是成绩列表
scores_dict = df.set_index('学号')['成绩'].to_dict() # 假设'学号'列是索引,'成绩'列是我们关心的成绩
def search_and_rank(student_id):
"""
输入学号,查询成绩并返回排名
"""
if student_id in scores_dict:
score = scores_dict[student_id]
# 对成绩降序排列
sorted_scores = sorted(scores_dict.values(), reverse=True)
rank = len([s for s in sorted_scores if s >= score]) + 1 # 如果当前成绩等于最高分,排名从一算起
return {'学号': student_id, '成绩': score, '排名': rank}
else:
return None # 返回None表示未找到该学生信息
# 使用示例
student_id_to_check = input("请输入你要查询的学生学号:")
result = search_and_rank(student_id_to_check)
if result is not None:
print(f"学号:{result['学号']}, 成绩:{result['成绩']}, 排名:{result['排名']}")
else:
print("找不到该学生的成绩信息。")
```
用Python代码设计一套学生成绩管理系 统(学生人数至少为10人),要求满足以下要求: 程序运行后,需要显示系统主菜单: --主菜单------- 1、 录入学生信息 查找学生信息 2- 3、 删除学生信息 4、 修改学生信息 5、 学生成绩统计 6 退出系统 按照主菜单内容,对学生成绩进行相关操作并给出对应显示, 这里给出示例:请选择功能:1请输入学生学号:请输入学生姓名:请输入学生高数成绩: 请输入学生英语成绩: 请输入学生Python 成绩: 请输入学生C语言成绩:请输入学生机器学习成绩: 录入完毕?[YN:(输入Y,则询问下一个选择的功能;输入 N,则开始输入下一个学生的信息和成绩。) 在功能2下,则可以根据学生姓名或者是学生学号查找学生信息, 并将其所有成绩进行输出显示。 在功能5下,需要进行: a)计算每个学生所有课程的总成绩和平均分, b)每一门课程中所有学生的平均分 c) 将所有汇总信息保存到一个.csv文件中
以下是基于Python的学生成绩管理系统的代码实现:
```python
import csv
class Student:
def __init__(self, id, name, math_score, eng_score, py_score, c_score, ml_score):
self.id = id
self.name = name
self.math_score = math_score
self.eng_score = eng_score
self.py_score = py_score
self.c_score = c_score
self.ml_score = ml_score
def get_total_score(self):
return self.math_score + self.eng_score + self.py_score + self.c_score + self.ml_score
def get_avg_score(self):
return self.get_total_score() / 5
class Scoresheet:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def search_student_by_id(self, id):
for student in self.students:
if student.id == id:
return student
return None
def search_student_by_name(self, name):
for student in self.students:
if student.name == name:
return student
return None
def remove_student_by_id(self, id):
for student in self.students:
if student.id == id:
self.students.remove(student)
return True
return False
def modify_student_by_id(self, id, name, math_score, eng_score, py_score, c_score, ml_score):
for student in self.students:
if student.id == id:
student.name = name
student.math_score = math_score
student.eng_score = eng_score
student.py_score = py_score
student.c_score = c_score
student.ml_score = ml_score
return True
return False
def get_total_avg_score(self):
total_score = 0
for student in self.students:
total_score += student.get_total_score()
return total_score / len(self.students)
def get_math_avg_score(self):
math_score = 0
for student in self.students:
math_score += student.math_score
return math_score / len(self.students)
def get_eng_avg_score(self):
eng_score = 0
for student in self.students:
eng_score += student.eng_score
return eng_score / len(self.students)
def get_py_avg_score(self):
py_score = 0
for student in self.students:
py_score += student.py_score
return py_score / len(self.students)
def get_c_avg_score(self):
c_score = 0
for student in self.students:
c_score += student.c_score
return c_score / len(self.students)
def get_ml_avg_score(self):
ml_score = 0
for student in self.students:
ml_score += student.ml_score
return ml_score / len(self.students)
def export_to_csv(self, filename):
with open(filename, 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['学号', '姓名', '高数成绩', '英语成绩', 'Python成绩', 'C语言成绩', '机器学习成绩'])
for student in self.students:
writer.writerow([student.id, student.name, student.math_score, student.eng_score, student.py_score, student.c_score, student.ml_score])
def get_input(prompt, input_type=None):
while True:
try:
if input_type:
value = input_type(input(prompt))
else:
value = input(prompt)
return value
except:
print('输入的数据类型不正确,请重新输入!')
scoresheet = Scoresheet()
while True:
print('---主菜单---')
print('1、录入学生信息')
print('2、查找学生信息')
print('3、删除学生信息')
print('4、修改学生信息')
print('5、学生成绩统计')
print('6、退出系统')
choice = get_input('请选择功能:', int)
if choice == 1:
while True:
id = get_input('请输入学生学号:')
name = get_input('请输入学生姓名:')
math_score = get_input('请输入学生高数成绩:', float)
eng_score = get_input('请输入学生英语成绩:', float)
py_score = get_input('请输入学生Python成绩:', float)
c_score = get_input('请输入学生C语言成绩:', float)
ml_score = get_input('请输入学生机器学习成绩:', float)
student = Student(id, name, math_score, eng_score, py_score, c_score, ml_score)
scoresheet.add_student(student)
is_finished = input('录入完毕?[Y/N]:')
if is_finished.lower() == 'y':
break
elif choice == 2:
while True:
search_type = get_input('请选择查找类型(1-学号,2-姓名):', int)
if search_type == 1:
id = get_input('请输入要查找的学生学号:')
student = scoresheet.search_student_by_id(id)
if student:
print(f'学号:{student.id},姓名:{student.name},高数成绩:{student.math_score},英语成绩:{student.eng_score},Python成绩:{student.py_score},C语言成绩:{student.c_score},机器学习成绩:{student.ml_score}')
else:
print('未找到该学生信息!')
elif search_type == 2:
name = get_input('请输入要查找的学生姓名:')
student = scoresheet.search_student_by_name(name)
if student:
print(f'学号:{student.id},姓名:{student.name},高数成绩:{student.math_score},英语成绩:{student.eng_score},Python成绩:{student.py_score},C语言成绩:{student.c_score},机器学习成绩:{student.ml_score}')
else:
print('未找到该学生信息!')
else:
print('输入的查找类型不正确,请重新输入!')
is_continue = input('是否继续查找?[Y/N]:')
if is_continue.lower() != 'y':
break
elif choice == 3:
while True:
id = get_input('请输入要删除的学生学号:')
if scoresheet.remove_student_by_id(id):
print('删除成功!')
else:
print('未找到该学生信息!')
is_continue = input('是否继续删除?[Y/N]:')
if is_continue.lower() != 'y':
break
elif choice == 4:
while True:
id = get_input('请输入要修改的学生学号:')
name = get_input('请输入学生姓名:')
math_score = get_input('请输入学生高数成绩:', float)
eng_score = get_input('请输入学生英语成绩:', float)
py_score = get_input('请输入学生Python成绩:', float)
c_score = get_input('请输入学生C语言成绩:', float)
ml_score = get_input('请输入学生机器学习成绩:', float)
if scoresheet.modify_student_by_id(id, name, math_score, eng_score, py_score, c_score, ml_score):
print('修改成功!')
else:
print('未找到该学生信息!')
is_continue = input('是否继续修改?[Y/N]:')
if is_continue.lower() != 'y':
break
elif choice == 5:
total_avg_score = scoresheet.get_total_avg_score()
math_avg_score = scoresheet.get_math_avg_score()
eng_avg_score = scoresheet.get_eng_avg_score()
py_avg_score = scoresheet.get_py_avg_score()
c_avg_score = scoresheet.get_c_avg_score()
ml_avg_score = scoresheet.get_ml_avg_score()
print(f'所有学生的总平均分:{total_avg_score:.2f}')
print(f'高数的平均分:{math_avg_score:.2f}')
print(f'英语的平均分:{eng_avg_score:.2f}')
print(f'Python的平均分:{py_avg_score:.2f}')
print(f'C语言的平均分:{c_avg_score:.2f}')
print(f'机器学习的平均分:{ml_avg_score:.2f}')
filename = get_input('请输入导出的文件名:')
scoresheet.export_to_csv(filename)
print(f'成绩已导出到{filename}文件中!')
elif choice == 6:
print('谢谢使用!')
break
else:
print('输入的菜单编号不正确,请重新输入!')
```
阅读全文