实现历史记录的功能

游戏描述:
  猜大小。系统随机生成一个整数(1,100)用户通过输入数据来猜测该整数的值;系统根据输入返回三种结果:too big, too small, you win.
  输入过程中可以查看最近输入了哪些数字,游戏结束后将历史记录值保存在文件’history’中。

from random import randint
from collections import deque
import pickle
import os

dest = randint(1, 100)
history = deque([], 6)
isExist = os.path.exists('history')
print(isExist)
if isExist:
    history = pickle.load(open('history', 'rb'))
    print(history)


def guess(a):
    if a > dest:
        print("input is too big")
        return False
    elif a < dest:
        print("input is too small")
        return False
    else:
        return True


while True:
    src = input("input your guess number:")
    if src.isdigit():
        numb = int(src)
        history.append(numb)
        if guess(numb):
            print("you win!")
            break
    elif src == 'h?':
        print(list(history))

pickle.dump(history, open('history', 'wb'))

 

posted on 2022-05-22 11:16  溪水静幽  阅读(61)  评论(0)    收藏  举报