zoukankan      html  css  js  c++  java
  • python学习--实现用户的历史记录功能deque

    场景:制作一个猜数字的小游戏,添加历史记录功能,显示用户最近输入的数字。

    from random import randint
    from collections import deque


    N=randint(0,100)
    history=deque([],5)#定义一个队列,最多存5个元素(先进先出)
    def isBingo(k):
      if k==N:
        print('right')
        return True
      elif k>N:
        print('太大了')
      else:
        print('太小了')

    while True:
      line=input('请输入0-100之间的一个整数:')
      if line.isdigit():  #方法检测字符串是否只由数字组成。
        k=int(line)  #转换成int类型
        history.append(k)
        print(history)
        if isBingo(k):
          break

    提升:存储python对象  用pickle

      存:pickle.dump(obj,open('history','w'))

      取: pickle.load(open('history'))

  • 相关阅读:
    Oracle中Lpad函数和Rpad函数的用法
    SQL中on条件与where条件的区别
    安装sqlServer
    springboot注解加深
    springcloud父项工程pom
    雪花算法
    docker 创建mysql容器
    docker入门
    restful接口规范(安全与幂等)
    mysql
  • 原文地址:https://www.cnblogs.com/daacheng/p/7912864.html
Copyright © 2011-2022 走看看