zoukankan      html  css  js  c++  java
  • 实现用户的历史记录功能

    实现用户的历史记录功能

    案例:
    很多应用程序都有浏览用户的历史记录的功能
    例如:
    浏览器查看最近访问的网页
    视频播放器查看最近播放过的视频文件
    shell查看用户输入的命令
    ......

    现制作一个简单的猜数字游戏,添加历史记录功能,显示最近猜过的数字

    解决思路:
    使用容量为n的队列存储历史记录
    使用标准库collections中的deque函数,它是一个双端循环队列

    from random import randint
    from collections import deque
    
    
    N = randint(0,100)
    history = deque([],6)
    
    def GuessNum(k):
    if k == N:
    print ("your guess right.")
    return True
    if k < N:
    print ("your guess number less-than N")
    else:
    print ("your guess number gteater-than N")
    return False
    
    while True:
    line = input("please input a number:").strip()
    if line.isdigit():
    k = int(line)
    history.append(k)
    if GuessNum(k):
    break
    elif line == "history" or line == "h?":
    print ("history:",history)
    
    please input a number:50
    your guess number gteater-than N
    please input a number:20
    your guess number less-than N
    please input a number:25
    your guess number less-than N
    please input a number:h?
    history: deque([50, 20, 25], maxlen=6)
    please input a number:2
    your guess number less-than N
    please input a number:32
    your guess number gteater-than N
    please input a number:28
    your guess number gteater-than N
    please input a number:h?
    history: deque([50, 20, 25, 2, 32, 28], maxlen=6)
    please input a number:25
    your guess number less-than N
    please input a number:26
    your guess right.
    

      

  • 相关阅读:
    2014.5.20知识点学习:void及void指针含义的深刻解析(转载)
    2014.5.20知识点学习:void与void*(转载)
    2014.5.19知识点学习:上下文切换
    编写“全选”按钮来操作大量复选框
    排序算法(冒泡排序,选择排序,插入排序,快速排序)
    算法基础
    Git &GitHub
    flask 上下文管理 &源码剖析
    rest-framework框架的基本组件
    Django的FBV和CB
  • 原文地址:https://www.cnblogs.com/xieshengsen/p/7208878.html
Copyright © 2011-2022 走看看