Python随机选取学生代码示例
以下是一个使用Python代码随机选取班里学生的示例:
import random
# 假设班里有5个学生
students = ['张三', '李四', '王五', '赵六', '钱七']
# 随机选出一个学生
selected_student = random.choice(students)
# 输出选中的学生
print('选中的学生是:', selected_student)
运行结果可能是:
选中的学生是: 李四
代码解释:
- 导入
random模块,该模块提供随机数生成功能。 - 创建一个包含学生姓名的列表
students。 - 使用
random.choice(students)函数从students列表中随机选择一个元素并赋值给selected_student变量。 - 使用
print()函数输出选中的学生姓名。
您可以根据实际情况修改students列表中的学生姓名。
原文地址: http://www.cveoy.top/t/topic/mzDa 著作权归作者所有。请勿转载和采集!