題目
每間隔5秒,隨機輸出10個不重復(fù)的4位數(shù)
實現(xiàn)思路
這里我們運用 Python
里面內(nèi)置模塊 random
和 time
來處理問題痪伦。
- 創(chuàng)建一個空集合
set()
,其元素不允許重復(fù) - 通過
random.randint(a, b)
方法癞蚕,用于生成指定范圍內(nèi)的整數(shù),生成的隨機數(shù)N:a <= N <= b - 通過集合
add()
方法桦山,將隨機數(shù)存儲到集合中 - 通過
time.sleep(5)
方法攒射,實現(xiàn)每隔5秒處理一次
代碼實現(xiàn)
import random, time
def random_number():
data = set()
while len(data) < 10:
data.add(random.randint(1000, 9999))
return data
while True:
print("本次輸出10個不重復(fù)的4位數(shù):{}".format(random_number()))
time.sleep(5)
更多Python編程題恒水,等你來挑戰(zhàn):Python編程題匯總(持續(xù)更新中……)