输入两个学生的学号、姓名和成绩,输出成绩较高的学生的学号、姓名和成绩。参考教材例9.2。
时间: 2023-12-16 09:06:14 浏览: 34
好的,这是一个简单的比较程序,以下是示例代码:
```python
stu1 = input("请输入第一个学生的学号、姓名和成绩,以空格分隔:")
stu2 = input("请输入第二个学生的学号、姓名和成绩,以空格分隔:")
stu1_list = stu1.split()
stu2_list = stu2.split()
if float(stu1_list[2]) > float(stu2_list[2]):
print("成绩较高的学生是:{} {} {}".format(stu1_list[0], stu1_list[1], stu1_list[2]))
else:
print("成绩较高的学生是:{} {} {}".format(stu2_list[0], stu2_list[1], stu2_list[2]))
```
请按照要求输入两个学生的信息,程序将自动比较两个学生的成绩,并输出成绩较高的学生的学号、姓名和成绩。
相关问题
输入两个学生的学号、姓名和成绩,输出成绩较高的学生的学号、姓名和成绩。参考教材例程序设计基础9.2。
好的,以下是参考代码:
```python
# 定义一个学生类
class Student:
def __init__(self, id, name, score):
self.id = id
self.name = name
self.score = score
# 获取学生信息
def get_student():
id = input("请输入学号:")
name = input("请输入姓名:")
score = float(input("请输入成绩:"))
return Student(id, name, score)
# 比较两个学生成绩,输出成绩较高的学生信息
def compare_score(student1, student2):
if student1.score > student2.score:
print("成绩较高的学生是:")
print("学号:", student1.id)
print("姓名:", student1.name)
print("成绩:", student1.score)
elif student1.score < student2.score:
print("成绩较高的学生是:")
print("学号:", student2.id)
print("姓名:", student2.name)
print("成绩:", student2.score)
else:
print("两个学生成绩相同!")
# 主函数
def main():
print("请输入第一个学生的信息:")
student1 = get_student()
print("请输入第二个学生的信息:")
student2 = get_student()
compare_score(student1, student2)
if __name__ == '__main__':
main()
```
你可以按照提示输入两个学生的信息,程序会自动比较两个学生的成绩,输出成绩较高的学生的信息。
输入两个学生的学号、姓名和成绩,输出成绩较高学生的学号、姓名和成
以下是一个示例代码,用于输入两个学生的学号、姓名和成绩,并输出成绩较高的学生的学号、姓名和成绩:
```python
class Student:
def __init__(self, student_id, name, score):
self.student_id = student_id
self.name = name
self.score = score
student1 = Student(input("请输入学生1的学号:"), input("请输入学生1的姓名:"), int(input("请输入学生1的成绩:")))
student2 = Student(input("请输入学生2的学号:"), input("请输入学生2的姓名:"), int(input("请输入学生2的成绩:")))
if student1.score > student2.score:
print("成绩较高的学生是:")
print("学号:", student1.student_id)
print("姓名:", student1.name)
print("成绩:", student1.score)
elif student1.score < student2.score:
print("成绩较高的学生是:")
print("学号:", student2.student_id)
print("姓名:", student2.name)
print("成绩:", student2.score)
else:
print("两个学生的成绩相等。")
print("学生1的信息:")
print("学号:", student1.student_id)
print("姓名:", student1.name)
print("成绩:", student1.score)
print("学生2的信息:")
print("学号:", student2.student_id)
print("姓名:", student2.name)
print("成绩:", student2.score)
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)