"""
本实例要求编写生词本程序
改程序具有查看生词列表、背单词、添加新单词、删除单词和清空生词本的功能。
"""
data_set = set()
print("="*20)
print("1.查看生词本")
print("2.背单词")
print("3.添加新单词")
print("4.删除单词")
print("5.清空单词本")
print("6.退出")
print("="*20)
while True:
word_data_dict = { } # 字典存储一个单词
fun_num = input("请输入功能编号:")
if fun_num == '1':
if len(data_set) == 0:
print("单词本内容为空!")
else:
print(data_set)
elif fun_num =='2':
if len(data_set) == 0:
print("单词本内容为空!")
else:
for random_words in data_set:
w = random_words.split(":")
in_words = input("请输入:"+w[1]+":\n")
if in_words == w[2].strip():
print("回答正确!")
else:
print("再想想!!")
elif fun_num == '3':
new_words = input("请输入新的单词:")
new_china = input("请输入单词翻译:")
# 新单词放入字典
word_data_dict.update({'单词':new_words,'翻译':new_china})
# 新单词放入我们的单词表(集合){'单词':'word','翻译':'字'}===》单词:word,翻译:字
dict_str = str(word_data_dict).replace("{","").replace("}","").replace("'","")
#.replace("}","").replace("’","")
print(dict_str)
# 将转换后的字符串存入生词本的集合
data_set.add(dict_str)
print("添加单词成功!")
elif fun_num == '4':
if len(data_set) == 0:
print("单词本内容为空!")
else:
list_1 = list(data_set)
print(list_1)
del_w = input("请输入要删除的单词:")
for i in list_1:
if del_w in i:
data_set.remove(i)
print("删除成功!")
break;
else:
print("请输入正确的单词!")
elif fun_num == '5':
if len(data_set) == 0:
print("单词本内容为空!")
else:
data_set.clear();
print("清空成功!")
elif fun_num == '6':
break;
all_word = set() # 设置集合
print("1、查看生词列表 \n2、背单词 \n3、添加新单词 \n4、删除单词 \n5、清空生词本 \n6、退出")
print('='*30)
while True:
word_dict = {}
chose_num = input("请输入功能序号:")
if chose_num == '1':
if len(all_word) == 0:
print("生词表无内容!")
else:
print(all_word)
elif chose_num == '2':
if len(all_word) == 0:
print('生词本内容为空')
else:
for random_words in all_word:
w = random_words.split(':')
in_words = input("请输入" + w[1] + ':\n')
print(in_words)
if in_words == w[2].strip():
print('太棒了')
else:
print('再想想')
elif chose_num == '3': # 3、添加新单词
# list_all_word = list(all_word)
new_word = input("请输入新单词:")
new_mean = input("请输入新单词的意思:")
word_dict.update({'单词': new_word, '翻译': new_mean})
# print(word_dict)
dict_str = str(word_dict).replace("|", "").replace("|", "").replace("'", "")
# print(word_dict)
# for i in list_all_word:
if dict_str in all_word:
print("此单词已存在")
else:
print(dict_str)
all_word.add(dict_str)
print("单词添加成功!")
elif chose_num == '4': # 4、删除单词
if len(all_word) == 0:
print("生词表为空!")
else:
del_list = list(all_word)
print(all_word)
del_word = input("请输入要删除的单词")
for i in del_list:
if del_word in i :
all_word.remove(del_word)
print("删除成功!")
break
else:
print("删除的单词不存在!")
elif chose_num == '5': # 5、清空生词本
if len(all_word) == 0:
print("单词本为空!")
else:
all_word.clear()
print("生词本已经清空!")
elif chose_num == '6':
break