Python使用字典实现的简单记事本功能示例


Posted in Python onAugust 15, 2019

本文实例讲述了Python使用字典实现的简单记事本功能。分享给大家供大家参考,具体如下:

from time import sleep, strftime
user = "Cytus"
calendar = {}
def welcome():
  print "Welcome to use this calendar. %s" % user
  print "Calendar is opening."
  sleep(1)
  print strftime("%A %B %d %Y")
  print strftime("%H:%M:%S")
  sleep(1)
  print "What would you like to do?"
def start_calendar():
  welcome()
  start = True
  while start:
    user_choice = raw_input("A to add, U to Update, V to View, D to Delete, X to Exit: ")
    user_choice = user_choice.upper()
    if user_choice == "V":
      if len(calendar.keys()) < 1:
        print "The calendar is empty."
      else:
        print calendar
    elif user_choice == "U":
      date = raw_input("What date? ")
      update = raw_input("Enter the update: ")
      calendar[date] = update
      print "Update successful."
      print calendar
    elif user_choice == "A":
      event = raw_input("Enter event: ")
      date = raw_input("Enter date (MM/DD/YYYY): ")
      if (len(date) > 10) or int(date[6:]) < int(strftime("%Y")):
        print "invaild date."
        try_again = raw_input("Try again? Y for yes, N for No: ")
        try_again = try_again.upper()
        if try_again == "Y":
          continue
        else:
          start = False
      else:
        calendar[date] = event
        print "Successfully added."
        print calendar
    elif user_choice == "D":
      if len(calendar.keys()) < 1:
        print "The calendar is empty."
      else:
        event = raw_input("What event? ")
        for date in calendar.keys():
          if calendar[date] == event:
            del calendar[date]
            print "Delete successfully."
            print calendar
          else:
            print "incorrect event was specified."
    elif user_choice == "X":
      start = False
    else:
      print "invalid input."
      start = False
start_calendar()

运行结果:

>>>
Welcome to use this calendar. Cytus
Calendar is opening.
Thursday August 15 2019
11:25:13
What would you like to do?
A to add, U to Update, V to View, D to Delete, X to Exit: A
Enter event: 3water.com
Enter date (MM/DD/YYYY): 08/15/2019
Successfully added.
{'08/15/2019': '3water.com'}
A to add, U to Update, V to View, D to Delete, X to Exit: V
{'08/15/2019': '3water.com'}
A to add, U to Update, V to View, D to Delete, X to Exit:
A to add, U to Update, V to View, D to Delete, X to Exit: X
>>>

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
Python迭代器和生成器介绍
Mar 06 Python
python中for语句简单遍历数据的方法
May 07 Python
Python使用ftplib实现简易FTP客户端的方法
Jun 03 Python
详解Python中的日志模块logging
Jun 19 Python
Python处理文本文件中控制字符的方法
Feb 07 Python
详解python中executemany和序列的使用方法
Aug 12 Python
快速入门python学习笔记
Dec 06 Python
python基于物品协同过滤算法实现代码
May 31 Python
Win10下Python3.7.3安装教程图解
Jul 08 Python
python操作微信自动发消息的实现(微信聊天机器人)
Jul 14 Python
Python爬虫开发与项目实战
Dec 16 Python
Django中的JWT身份验证的实现
May 07 Python
Flask框架学习笔记之模板操作实例详解
Aug 15 #Python
Flask框架学习笔记之消息提示与异常处理操作详解
Aug 15 #Python
python打造爬虫代理池过程解析
Aug 15 #Python
使用selenium和pyquery爬取京东商品列表过程解析
Aug 15 #Python
如何爬取通过ajax加载数据的网站
Aug 15 #Python
Python K最近邻从原理到实现的方法
Aug 15 #Python
Python数据可视化 pyecharts实现各种统计图表过程详解
Aug 15 #Python
You might like
用PHP和MySQL保存和输出图片
2006/10/09 PHP
如何解决CI框架的Disallowed Key Characters错误提示
2013/07/05 PHP
CI框架给视图添加动态数据
2014/12/01 PHP
Laravel框架Request、Response及Session操作示例
2019/05/06 PHP
javascript 鼠标拖动图标技术
2010/02/07 Javascript
Jquery升级新版本后选择器的语法问题
2010/06/02 Javascript
js控制frameSet示例
2013/09/10 Javascript
jQuery大于号(&gt;)选择器的作用解释
2015/01/13 Javascript
jQuery实现图片预加载效果
2015/11/27 Javascript
基于Bootstrap3表格插件和分页插件实例详解
2016/05/17 Javascript
indexedDB bootstrap angularjs之 MVC DOMO (应用示例)
2016/06/20 Javascript
jQuery中ScrollTo用法示例
2016/09/04 Javascript
BootStrap中
2016/12/10 Javascript
JS基于递归实现网页版计算器的方法分析
2017/12/20 Javascript
使用live-server快速搭建本地服务器+自动刷新的方法
2018/03/09 Javascript
小程序两种滚动公告栏的实现方法
2019/09/17 Javascript
vue.js+ElementUI实现进度条提示密码强度效果
2020/01/18 Javascript
使用Python进行稳定可靠的文件操作详解
2013/12/31 Python
一个检测OpenSSL心脏出血漏洞的Python脚本分享
2014/04/10 Python
Python基于Tkinter实现的记事本实例
2015/06/17 Python
TFRecord文件查看包含的所有Features代码
2020/02/17 Python
如何通过Python3和ssl实现加密通信功能
2020/05/09 Python
浅谈tensorflow 中的图片读取和裁剪方式
2020/06/30 Python
python dir函数快速掌握用法技巧
2020/12/09 Python
Css3新特性应用之视觉效果实例
2016/12/12 HTML / CSS
Marriott国际:万豪国际酒店查询预订
2017/09/25 全球购物
南京某公司笔试题
2013/01/27 面试题
教师群众路线教育实践活动学习笔记
2014/11/05 职场文书
2015年精神文明建设工作总结
2015/04/21 职场文书
被告答辩状范文
2015/05/22 职场文书
高效笔记技巧分享:学会这些让你不再困扰
2019/09/04 职场文书
MySQL 使用SQL语句修改表名的实现
2021/04/07 MySQL
Python集合的基础操作
2021/11/01 Python
CSS3实现360度循环旋转功能
2022/02/12 HTML / CSS
Redis 异步机制
2022/05/15 Redis
MyBatis在注解上使用动态SQL方式(@select使用if)
2022/07/07 Java/Android