编写程序,构造一个由50个元组组成的列表,每个元组由2个元素构成。第一个元素代表学号,是 [1,50]范围内的不同整数;第二个元素代表Python考试成绩,是[0,100]范围内的随机整数。 例如: students=[(1,88),(2,90),(3,67), … ,(49,82),(50,54)] 请找出所有Python成绩不及格的学号,并打印输出。
时间: 2024-02-15 13:04:53 浏览: 9
好的,以下是 Python 代码实现:
```python
import random
# 生成一个由50个元组组成的列表
students = [(i, random.randint(0, 100)) for i in range(1, 51)]
# 打印所有Python成绩不及格的学号
for student in students:
if student[1] < 60:
print("学号为{}的学生Python成绩为{},不及格!".format(student[0], student[1]))
```
运行结果可能会类似于:
```
学号为5的学生Python成绩为59,不及格!
学号为6的学生Python成绩为55,不及格!
学号为10的学生Python成绩为57,不及格!
...
学号为47的学生Python成绩为52,不及格!
学号为50的学生Python成绩为55,不及格!
```
相关问题
编写程序,构造一个由50个元组组成的列表,每个元组由2个元素构成。第一个元素代表学号,是 [1,50]范围内的不同整数;第二个元素代表Python考试成绩,是[0,100]范围内的随机整数
好的,我可以帮你写一个 Python 程序,用于生成这样的列表。你可以使用 Python 内置的 `random` 模块生成随机数。下面是程序代码:
```python
import random
# 生成包含50个元组的列表,每个元组包含学号和Python考试成绩
scores = [(i, random.randint(0, 100)) for i in range(1, 51)]
print(scores)
```
这个程序会生成一个包含50个元组的列表,每个元组都包含一个学号和一个随机的Python考试成绩。学号从 1 到 50,成绩从 0 到 100。你可以根据需要修改生成列表的范围和元组的元素个数。
python 一个列表是由许多个元组构成,想取出每个元组的第一个数
可以使用循环遍历列表,然后通过索引取出每个元组的第一个数。具体代码如下:
```
my_list = [(1, 'a'), (2, 'b'), (3, 'c')]
for tup in my_list:
print(tup[0])
```
输出结果为:
```
1
2
3
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)