pickle默认操作二进制文件,使用文件函数的时候需要注意,否则出现 TypeError
如下,open
函数参数更改为 wb
可以正常运行
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# 实现用户的历史记录功能
# 使用容量为 n 的队列结构
from collections import deque
from random import randint
import pickle
import os
# 队列的初始值,容量
# history = deque([], 5)
history = deque()
if os.path.exists("./history"):
history = pickle.load(open("history", "rb"))
else:
histoty = deque([], 5)
# N = randint(0, 100)
N = 60
def guess(k):
if k == N:
print('Right')
return True
if k < N:
print(str(k) + " is less-than N")
else:
print(str(k) + " is greater-than N")
return False
while True:
line = input('Please input a number: ')
if line.isdigit():
k = int(line)
history.append(k)
if guess(k):
break
elif line == 'history' or line == "h?":
print(list(history))
pickle.dump(history, open("history", "wb"))