热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

背单词系统Python

#背单词系统用文件系统和面向对象的方式classdanci():defnew_del(self,a):#删除syingaflagFalsewithopen(danciben.txt

#背单词系统
用文件系统和面向对象的方式

class danci():

def new_del(self,a):#删除
sying = a
flag = False
with open('danciben.txt','r') as f1:
lines = f1.readlines()
with open('danciben.txt','w+') as f2:
for line in lines:
k =line.split('-')[0]
if sying == k:
flag = True
continue
f2.write(line)
if flag == True:
print('删除成功!')
if flag == False:
print("未有该单词")
def new_add(self,y,c):#添加
ying = y
chOng= c
liu = y + '-' + c + '\n'
flag = True
with open('danciben.txt','r') as f:
line = f.readlines()
for index in line:
h = index.split('-')[0]
if y == h:
print("该单词已经在单词本")
flag==False
if flag==True:
with open('danciben.txt','a+') as file:
file.writelines(liu)
def new_show(self):#查看所有单词
print("英文-中文")
with open('danciben.txt','r') as f3:
line1 = f3.readlines()
for line2 in line1:
print(line2)
print("查询成功!")
def new_clear(self):#清空
with open('danciben.txt','w') as f4:
pass
def new_gebieshow1(self,v):#查询汉语意思
yingwen = v
flag = False
with open('danciben.txt','r') as files:
line = files.readlines()
for index in line:
h = index.split('-')[0]
if yingwen == h:
print(index)
flag = True
if flag==False:
print("单词本里未有该单词")

def new_gebieshow2(self,v):#查询英文
hanyu = v
flag = False
with open('danciben.txt','r') as files:
line = files.readlines()
for index in line:
o = index.split('-')[1]
if hanyu + '\n' == o:
print(index)
flag = True
if flag==False:
print("单词本里未有该单词")
def Backword(self):#背单词
#new_worldFile = []
with open("danciben.txt", 'r') as worldFile:
world_list = worldFile.readlines()
for index in world_list:
w = index.split("-")[0]
c = index.split("-")[1]
count = 1
flag = True
while count <= 3 and flag == True:
en = input(str(w) + "的汉语意思:")
if en + "\n" == c:
print("恭喜答对")
flag = False
if en + "\n" != c:
print("答错了,还有"+str(3-count)+"次机会:")
count = count + 1
flag = True
print("您已经完成了单词本里的全部单词")

def Menu():#主菜单
print(’&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;’)
print(’* 【单词王】 ‘)
print(’
1.添加生词 ‘)
print(’
2.删除生词 ‘)
print(’
3.查看生词 ‘)
print(’
4.清空生词 ‘)
print(’
5.查询生词 ‘)
print(’
6.背 生 词 ‘)
print(’
7.退 出 ‘)
print(’
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;’)
Menu()
dc = danci()
print(&#8220;
-欢迎使用背单词软件APP-*&#8221;)
while True:
i = int(input(“输入您要选择的功能(1-7):”))
if i == 1:
y = input(“输入生词的英文:”)
c = input(“输入生词的中文:”)
dc.new_add(y,c)
if i == 2:
a = input(“输入生词的英文:”)
dc.new_del(a)
if i == 3:
dc.new_show()
if i == 4:
dc.new_clear()
if i == 5:
b = input(“输入查询中文or查询英文:”)
if b == “查询中文”:
v = input(“请输入英文单词”)
dc.new_gebieshow1(v)
if b == “查询英文”:
v = input(“请输入该单词中文意思”)
dc.new_gebieshow2(v)
if i == 6:
dc.Backword()
if i == 7:
print(“确定要退出该程序吗?”)
u = input(“确定or再背会:”)
if u == “确定”:
break


推荐阅读
author-avatar
热情风吟_181
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有