zoukankan      html  css  js  c++  java
  • 新猜数字游戏--查看历史记录

    猜数字游戏,就是随机生成一个数字,猜这个数字的大小,输入的值会有提醒比真值大还是小。这个新是说通过加入队列来实现了可以查看之前输入过得值。

     1 # encoding = utf-8
     2 from random import randint
     3 # 引入队列
     4 from collections import deque 
     5 
     6 N = randint(0,100)
     7 # history队列能够存储5个值
     8 history = deque([],5)
     9 
    10 def guess(k):
    11     if k == N:
    12         print 'right'
    13         return True
    14     if k < N:
    15         print '%s is less-than N' % k
    16     else:
    17         print '%s is bigger-than N' % k
    18     return False
    19 
    20 while True:
    21     line = raw_input("please input a number:")
    22     if line.isdigit():
    23         k = int(line)
    24         # 通过append方法将新输入的值存储到history中
    25         history.append(k)
    26         if guess(k):
    27             break
    28     # 通过输入相应的字符来查看输入历史
    29     elif line == 'history' or line == 'h?':
    30         print list(history)
    31             
    32             
    33         

    这种方式只能暂时将数值保存到相应的程序中,当再次打开程序时就会作废。可以利用pickle包中的函数来实现将信息存储到文件或者从文件中读取信息。

    1 # 引入包
    2 import pickle
    3 # 打开文件并将信息写入
    4 pickle.dump(q,open('history','w'))
    5 # 从文件中读取并赋值给q2
    6 q2 = pickle.load(open('history'))
  • 相关阅读:
    Redis其他知识
    Mybatis的sql语句操作
    Redis
    mybatis插件原理
    mybatis工作原理
    Mybatis逆向工程
    mybatis缓存机制
    Mybatis查询
    zabbix api添加主机
    jenkins + bitbucket 实现 pr自动构建及build状态通知
  • 原文地址:https://www.cnblogs.com/banshaohuan/p/6918826.html
Copyright © 2011-2022 走看看