zoukankan      html  css  js  c++  java
  • 如何实现输入历史记录功能

    现在需求是,在一些填写表单,或者输入场景时,下次输入有一个自动提示,他上次输入过的内容,从乐自动提示,保存三个内容,当输入次数过多,就只更新三内容,节省内存,现在以一个儿、猜数字游戏为例子,当用户猜数字时,猜了几次后,他猜过的数字他都忘了,可以输 入类似于 help?查看输入的最近五次内容

    代码如下

    from random import randint
    n = randint(0,100)
    #这个猜数字游戏牛逼了,连我自己也不知道数字是多,怎么猜
    def guess(k):
    if k == n:
    print("right")
    return True
    if k<n:
    print("%s is les.htan n" %k)
    else:
    print("%s is greater-than n" %k)
    return False
    while True:
    line = input("please input a number")
    if line.isdigit():#测是不是数字
    k = int(line)
    if guess(k):
    break
    当猜的次数过多,我自己都不知道曾猜过那些数字,所以需要开发一个帮助程序,开记录提示猜过的数字
    版本二
    使用标准库的collections的deque 来实现容量为n的队列存储历史记录
    from random import randint
    from collections import deque

    n = randint(0,100)
    q=deque([],5)#初始空,大小为5
    #这个猜数字游戏牛逼了,连我自己也不知道数字是多,怎么猜
    def guess(k):
    if k == n:
    print("right")
    return True
    if k<n:
    print("%s is les.htan n" %k)
    else:
    print("%s is greater-than n" %k)
    return False
    while True:
    line = input("please input a number")
    if line.isdigit():#测是不是数字
    k = int(line)
    q.append(k)
    if guess(k):
    break
    #如果输入的是帮助,就提示输入历史
    elif line == "help" or line == "h?":
    print(list(q))
    '''
    运行结果
    please input a number0
    0 is les.htan n
    please input a number4
    4 is les.htan n
    please input a numberh?
    [0, 4]
    please input a number
    '''
  • 相关阅读:
    JFinal项目eclipse出现the table mapping of model: com.gexin.model.scenic.Scenic not exists or the ActiveRecordPlugin not start.
    mysql图形化工具获取表的源码
    数据库中字段是什么意思?
    myeclipse出现Failed to load JavaHL Library.
    1002.查找常用字符
    JS中的AO 和 VO 闭包
    制作icon图标
    babel
    递归时间复杂度
    js函数的柯里化和偏函数
  • 原文地址:https://www.cnblogs.com/fgxwan/p/9576761.html
Copyright © 2011-2022 走看看