常用标准库 random,python 入门教程之每日 5 or 6 道题 | Python技能树征题

本文通过一系列题目介绍了Python的random库,包括如何随机选择1~100之间的数字,生成六位数字验证码,随机获取User-Agent,以及在限定范围内抽取不重复数字等应用场景,适合初学者巩固random库的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本篇博客主要为 https://bbs.youkuaiyun.com/skill/python 频道练习题模块补充题目,暂定每天提供 5 or 6 道测试题,后面可能会更多哦~。

本篇博客对【进阶语法】→ 【常用标准库】 进行出题。

以下题目,默认将正确答案,放置在选项 A 位置

知识点:python random 库

第 1 题:

题目难度:1 星
题干(问题描述):
下述哪个选项能在 1~100 中的随机选择 5 个数字

选项 A:

import random

for i in range(1, 6):
    rand_num = random.randint(1, 100)
    print(rand_num)

选项 B:

import random

for i in range(1, 6):
    rand_num = random.random(1, 100)
    print(rand_num)

选项 C:

import random

for i in range(1, 6):
    rand_num = random.sample(1, 100)
    print(rand_num)

选项 D:

import random

for i in range(1, 6):
    rand_num = random.randint(1, 101)
    print(rand_num)

正确答案:A

第 2 题:

题目难度:1 星
题干(问题描述):
编写程序,在程序中随机为用户生成 6 位数短信验证码,包含大写字母。

选项 A:

import random

password = []
for i in range(3):
    num = random.randint(0, 9)
    password.append(str(num))
    char_num = random.randint(65, 90)
    password.append(chr(char_num))

print("".join(password))

选项 B:

import random

password = []
for i in range(6):
    num = random.randint(0, 9)
    password.append(str(num))
    char_num = random.randint(65, 90)
    password.append(chr(char_num))

print("".join(password))

选项 C:

import random

password = []
for i in range(3):
    num = random.randint(0, 9)
    password.append(str(num))
    char_num = random.randint(97,122)
    password.append(chr(char_num))

print("".join(password))

选项 D:

import random

password = []
for i in range(3):
    num = random.randint(0, 9)
    password.append(str(num))
    char_num = random.randint(97,122)
    password.append(chr(num ))

print("".join(password))

正确答案:A

第 3 题:

题目难度:2 星
题干(问题描述):
爬虫代码编写中,会随机获取用户代理值,即 User-Agent,编写函数,实现随机从列表中获取 User-Agent

选项 A:

import random


def get_headers():
    uas = [
        "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)",
        "Mozilla/5.0 (compatible; Baiduspider-render/2.0; +http://www.baidu.com/search/spider.html)",
        "Baiduspider-image+(+http://www.baidu.com/search/spider.htm)",
        "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 YisouSpider/5.0 Safari/537.36",
    ]
    ua = random.choice(uas)
    headers = {
        "user-agent": ua
    }
    return headers

if __name__ == '__main__':
    print(get_headers())

选项 B:

import random


def get_headers():
    uas = [
        "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)",
        "Mozilla/5.0 (compatible; Baiduspider-render/2.0; +http://www.baidu.com/search/spider.html)",
        "Baiduspider-image+(+http://www.baidu.com/search/spider.htm)",
        "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 YisouSpider/5.0 Safari/537.36",
        "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
        "Mozilla/5.0 (compatible; Googlebot-Image/1.0; +http://www.google.com/bot.html)",
        "Sogou web spider/4.0(+http://www.sogou.com/docs/help/webmasters.htm#07)",
        "Sogou News Spider/4.0(+http://www.sogou.com/docs/help/webmasters.htm#07)",
        "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0);",
        "Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)",
        "Sosospider+(+http://help.soso.com/webspider.htm)",
        "Mozilla/5.0 (compatible; Yahoo! Slurp China; http://misc.yahoo.com.cn/help.html)"
    ]
    ua = random.random(uas)
    headers = {
        "user-agent": ua
    }
    return headers

if __name__ == '__main__':
    print(get_headers())

选项 C:

import random


def get_headers():
    uas = [
        "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)",
        "Mozilla/5.0 (compatible; Baiduspider-render/2.0; +http://www.baidu.com/search/spider.html)",
        "Baiduspider-image+(+http://www.baidu.com/search/spider.htm)",
        "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 YisouSpider/5.0 Safari/537.36",
        "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
        "Mozilla/5.0 (compatible; Googlebot-Image/1.0; +http://www.google.com/bot.html)",
        "Sogou web spider/4.0(+http://www.sogou.com/docs/help/webmasters.htm#07)",
        "Sogou News Spider/4.0(+http://www.sogou.com/docs/help/webmasters.htm#07)",
        "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0);",
        "Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)",
        "Sosospider+(+http://help.soso.com/webspider.htm)",
        "Mozilla/5.0 (compatible; Yahoo! Slurp China; http://misc.yahoo.com.cn/help.html)"
    ]
    ua = random.sample(uas)
    headers = {
        "user-agent": ua
    }
    return headers

if __name__ == '__main__':
    print(get_headers())

选项 D:

import random


def get_headers():
    uas = [
        "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)",
        "Mozilla/5.0 (compatible; Baiduspider-render/2.0; +http://www.baidu.com/search/spider.html)",
        "Baiduspider-image+(+http://www.baidu.com/search/spider.htm)",
        "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 YisouSpider/5.0 Safari/537.36",
        "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
        "Mozilla/5.0 (compatible; Googlebot-Image/1.0; +http://www.google.com/bot.html)",
        "Sogou web spider/4.0(+http://www.sogou.com/docs/help/webmasters.htm#07)",
        "Sogou News Spider/4.0(+http://www.sogou.com/docs/help/webmasters.htm#07)",
        "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0);",
        "Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)",
        "Sosospider+(+http://help.soso.com/webspider.htm)",
        "Mozilla/5.0 (compatible; Yahoo! Slurp China; http://misc.yahoo.com.cn/help.html)"
    ]
    ua = random.shuffle(uas)
    headers = {
        "user-agent": ua
    }
    return headers

if __name__ == '__main__':
    print(get_headers())

正确答案:A

第 4 题:

题目难度:2 星
题干(问题描述):
在 1000 内,随机获取 6 个整数,要求各个数字互不重复。

选项 A:

from random import sample

ret = sample(range(1000), 6)
print(ret)

选项 B:

from random import sample

ret = random.sample(range(1000), 6)
print(ret)

选项 C:

import random

ret = random.shuffle(range(1000), 6)
print(ret)

选项 D:

import random

for i in range(1, 6):
    rand_num = random.randint(1, 1000)
    print(rand_num)

正确答案:A

第 5 题:

题目难度:3 星
题干(问题描述):
随机生成一个由 10 组成的列表(长度要求为 20),其中 0 的个数不能超过 5 个。

选项 A:

import random

zero_max = 5
my_list = [1] * 20

zero_list = random.randint(0, zero_max)

zero_pos = random.sample(range(20), zero_list)
for pos in zero_pos:
    my_list[pos] = 0
print(my_list)

选项 B:

import random

zero_max = 5
my_list = [1] * 20

zero_list = random.randint(0, zero_max)

zero_pos = random.sample(range(10), zero_list)
for pos in zero_pos:
    my_list[pos] = 0
print(my_list)

选项 C:

import random

zero_max = 5
my_list = [0] * 20

zero_list = random.randint(0, zero_max)

zero_pos = random.sample(range(10), zero_list)
for pos in zero_pos:
    my_list[pos] = 0
print(my_list)

选项 D:

import random

zero_max = 5
my_list = [0] * 20

zero_list = random.randint(0, zero_max)

zero_pos = random.sample(range(10), zero_list)
for pos in zero_list:
    my_list[pos] = 0
print(my_list)

正确答案:A

试题仓库地址如下:

https://codechina.youkuaiyun.com/hihell/question

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦想橡皮擦

如有帮助,来瓶可乐

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值