Python使用字典實現(xiàn)的簡單記事本功能示例
本文實例講述了Python使用字典實現(xiàn)的簡單記事本功能。分享給大家供大家參考,具體如下:
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()
運行結(jié)果:
>>>
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: www.dbjr.com.cn
Enter date (MM/DD/YYYY): 08/15/2019
Successfully added.
{'08/15/2019': 'www.dbjr.com.cn'}
A to add, U to Update, V to View, D to Delete, X to Exit: V
{'08/15/2019': 'www.dbjr.com.cn'}
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
>>>
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python字典操作技巧匯總》、《Python列表(list)操作技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python字符串操作技巧匯總》及《Python入門與進階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
手把手教你jupyter?notebook更換環(huán)境的方法
在日常使用jupyter-notebook時,可能會碰到需要切換不同虛擬環(huán)境的場景,下面這篇文章主要給大家介紹了關(guān)于jupyter?notebook更換環(huán)境的方法,需要的朋友可以參考下2023-05-05
Python使用Pickle庫實現(xiàn)讀寫序列操作示例
這篇文章主要介紹了Python使用Pickle庫實現(xiàn)讀寫序列操作,結(jié)合實例形式分析了pickle模塊的功能、常用函數(shù)以及序列化與反序列化相關(guān)操作技巧,需要的朋友可以參考下2018-06-06
Python實現(xiàn)Event回調(diào)機制的方法
今天小編就為大家分享一篇Python實現(xiàn)Event回調(diào)機制的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-02-02
Django JWT Token RestfulAPI用戶認證詳解
這篇文章主要介紹了Django JWT Token RestfulAPI用戶認證詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01

