题目

每间隔5秒,随机输出10个不重复的4位数

实现思路

这里我们运用 Python 里面内置模块 randomtime 来处理问题。

  • 创建一个空集合 set() ,其元素不允许重复
  • 通过 random.randint(a, b) 方法,用于生成指定范围内的整数,生成的随机数N:a <= N <= b
  • 通过集合 add() 方法,将随机数存储到集合中
  • 通过 time.sleep(5) 方法,实现每隔5秒处理一次

代码实现

import random, time

def random_number():
    data = set()
    while len(data) < 10:
        data.add(random.randint(1000, 9999))
    return data

while True:
    print("本次输出10个不重复的4位数:{}".format(random_number()))
    time.sleep(5)

更多Python编程题,等你来挑战:Python编程题汇总(持续更新中……)

相关文章:

  • 2022-12-23
  • 2021-12-22
  • 2021-08-01
  • 2021-06-26
  • 2022-01-11
  • 2021-09-14
  • 2021-11-15
猜你喜欢
  • 2021-06-23
  • 2022-12-23
  • 2021-07-25
  • 2022-12-23
  • 2022-12-23
  • 2021-09-03
  • 2022-02-19
相关资源
相似解决方案