標準庫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個數