实现一个简单的单词本

功能:

可以添加单词和词义,当所添加的单词已存在有相应提示

可以查找单词,当查找的单词不存在时有相应提示

可以删除单词,当删除的单词不存在时有相应提示

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

dict = {}

while 1:

command = input("Please input your command: ")

if command == "add":

new_word = input("Please input the word want to add: ")

if new_word in dict:

print("The word already exist.")

continue

meaning = input("Please input the corresponding meaning to the word: ")

dict[new_word] = meaning

print("Thanks! The following new word has been added: %s" % new_word)

print("Continue input your command.")

elif command == "search":

keyword = input("Please input the word want to search: ")

if keyword in dict:

print("Found it!")

else:

print("Not found.")

print("Continue input your command.")

elif command == "delete":

word_for_delete = input("Please input the word want to delete: ")

if word_for_delete not in dict:

print("Not found. Not able to delete.")

else:

dict.pop(word_for_delete)

print("%s is deleted successfully." % word_for_delete)

print("Continue input your command.")

elif command == "bye":

print("Exit the program.")

break

else:

print("Wrong command. Please input again.")

continue

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容