python中从键盘输入10个人的成绩,并升序,降序排列
时间: 2023-05-25 15:05:22 浏览: 71
# 从键盘输入10个人的成绩
scores = []
for i in range(10):
score = int(input("请输入第" + str(i + 1) + "位同学的成绩:"))
scores.append(score)
# 升序排列
scores.sort()
print("成绩升序排列:", scores)
# 降序排列
scores.reverse()
print("成绩降序排列:", scores)
相关问题
python中从键盘输入10个人的成绩
以下是用Python实现从键盘输入10个人的成绩的代码:
```python
scores = [] # 创建一个空列表用于存储成绩
for i in range(1, 11):
score = float(input("请输入第%s个人的成绩:" % i))
scores.append(score)
print("全部成绩为:", scores)
```
执行后,程序将提示你输入10个人的成绩,然后把这些成绩存储在一个列表中,并打印输出。
利用python内置函数对列表进行升序和降序排列
可以使用Python内置函数`sorted()`和`sort()`来对列表进行升序和降序排列。
`sorted()`函数会返回一个新的已排序的列表,而`sort()`函数则会直接改变原列表的顺序。
以下是对列表进行升序和降序排列的示例代码:
```python
# 创建一个列表 x
x = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
# 对 x 进行升序排列
x_sorted = sorted(x)
print(x_sorted) # 输出 [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]
# 对 x 进行降序排列
x_sorted_reverse = sorted(x, reverse=True)
print(x_sorted_reverse) # 输出 [9, 6, 5, 5, 5, 4, 3, 3, 2, 1, 1]
# 直接对 x 进行升序排列
x.sort()
print(x) # 输出 [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]
# 直接对 x 进行降序排列
x.sort(reverse=True)
print(x) # 输出 [9, 6, 5, 5, 5, 4, 3, 3, 2, 1, 1]
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)