1.实现一个简单的单词本,功能:

①添加单词,当所添加的单词已存在时,让用户知道

②查找单词,当查找的单词不存在时,让用户知道

③删除单词,当删除的单词不存在时,让用户知道

以上功能可以无限次操作,直到用户输入bye退出程序

1 #如何接收输入的help项
 2 #coding:utf-8
 3 help='''
 4     1.add a word
 5     2.find a word
 6     3.delete a word
 7     input bye to exit
 8     '''
 9 print (help)
10 import sys
11 wordbook=[]
12 while(1):
13     command=input("please input your command:")
14     if command=="1":
15         word=(input("please input your the word you want to add:")).strip()
16         if word not in wordbook:
17             wordbook.append(word)
18         print ("the word already exists")
19     if command=="2":
20         word=(input("please input your the word you want to find:")).strip()
21         for i in wordbook:
               if i==word:
24                 print ("find it")
25             print ("words do not exist.")
26     if command=="3":
27         word=(input("please input your the word you want to delete:")).strip()
28         for i in wordbook:
29             if i==word:
30                 wordbook.remove(i)
31                 print ("Word deleted")
32             print ("words do not exist.")
33     if command=="bye":
34         sys.exit()

 

2.升级版,单词本类型为字典,用封装函数的方法来实现

1 #如何接收输入的help项
 2 #coding:utf-8
 3 def addword():
 4     word=(input("请输入你要添加的单词:")).strip()
 5     if word in wordbook.keys():
 6         print ("the word already exists")
 7     else:
 8         word_meaning=input("请输入单词的含义:")
 9         wordbook[word]=word_meaning
10         print ("单词添加成功")
11     print ("最新的单词本为:",wordbook)
12 def findword():
13     word=(input("请输入你要查找的单词:")).strip()
14     if word in wordbook.keys():
15         print ("您查找的单词已存在,单词的含义是:",wordbook[word])
16     else:
17         print ("没有该单词")
18 def deleteword():
19     word=(input("please input your the word you want to delete:")).strip()
20     if word in wordbook.keys():
21         del wordbook[word]
22         print ("单词删除成功")
23     else:
24         print ("没有该单词")
25     print ("最新的单词本为:",wordbook)
26 
27 help='''
28     1.add a word
29     2.find a word
30     3.delete a word
31     input bye to exit
32     '''
33 print (help)
34 import sys
35 wordbook={}
36 
37 while(1):
38     command=input("please input your command:")
39     if command=="1":
40         addword()
41     if command=="2":
42         findword()
43     if command=="3":
44         deleteword()
45     if command=="bye":
46         sys.exit()