标准库collections的deque,是一个双端循环队列,可以限制存储容量
from random import randint
from collections import deque
his = []
his2 = deque([], 5)#限制长度为5
N = randint(0,100)
def guess(k):
if k == N :
print("猜正确!")
return True
if k > N :
print("数字猜大了!")
else:
print("数字猜小了!")
return False
while True:
line = input("input a number:")
if line.isdigit():
k = int(line)
his2.append(k)
his.append(k)
if guess(k):
break
his:[10, 34, 23, 26, 2, 28]
his2:deque([34, 23, 26, 2, 28])#始终为最新的5个数
版权声明:本文为ayangann915原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。