python学习--实现用户的历史记录功能deque

场景:制作一个猜数字的小游戏,添加历史记录功能,显示用户最近输入的数字。

from random import randint
from collections import deque


N=randint(0,100)
history=deque([],5)#定义一个队列,最多存5个元素(先进先出)
def isBingo(k):
  if k==N:
    print('right')
    return True
  elif k>N:
    print('太大了')
  else:
    print('太小了')

while True:
  line=input('请输入0-100之间的一个整数:')
  if line.isdigit():  #方法检测字符串是否只由数字组成。
    k=int(line)  #转换成int类型
    history.append(k)
    print(history)
    if isBingo(k):
      break

提升:存储python对象  用pickle

  存:pickle.dump(obj,open('history','w'))

  取: pickle.load(open('history'))

原文地址:https://www.cnblogs.com/daacheng/p/7912864.html