Python——实现历史记录的功能

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

from random import randint
from collections import deque
import pickle
history = deque([], 6)
dest = randint(1, 100)
def guess(a) :
	if a > dest :
		print('input is too big')
		return False
	if a < dest :
		print('input is too small')
		return False
	else :
		return True

while True :
	src = input('please guess the dest number:')
	if src.isdigit() :
		history.append(src)
		if guess(int(src)) :
			print('congratulationgs! you win!')
			break
	elif src == 'h?' :
		print(history)
	else :
		print('invalid input, try again')

print('game is over')
pickle.dump(history, open('history', 'w+b'))

运行结果:

please guess the dest number:1
input is too small
please guess the dest number:80
input is too small
please guess the dest number:90
input is too big
please guess the dest number:85
input is too small
please guess the dest number:87
input is too small
please guess the dest number:86
input is too small
please guess the dest number:88
congratulationgs! you win!
game is over

你可能感兴趣的:(python,开发语言,后端)