python 2-7如何实现用户的历史记录功能(最多n条)

猜数字的游戏,利用pickle存储历史数据,利用raw_input获取输入数据,利用deque双向队列实现数据的排队

from random import randint
from collections import deque
from code import InteractiveConsole
import pickle

N = randint(0, 100)
history = deque([], 5)


def guess(k):
    if k == N:
        print("you are right")
        return True
    elif k > N:
        print("num is lower than %s" % k)
    elif k < N:
        print("num is greater than %s" % k)
    return False


while (True):

    try:
        history = pickle.load(open("./history.txt", "rb"))
    except:
        pass

    line = InteractiveConsole.raw_input("please input a num")
    if line.isdigit():
        kk = int(line)
        history.append(line)
        pickle.dump(history, open("./history.txt", "wb"))
        if guess(kk):
            break
        else:
            continue
    elif line == 'history' or line == 'h?':
        print(list(history))
    elif line == 'quit' or line == 'exit' or line == "q?":
        break