用python写一个背单词程序

##########################################################################################
#  1.本程序主要用途是背单词,主要有五个功能,添加新单词,查询模式,默写中文,默写英文,背诵模式
#    (1)添加功能:先添加英文,然后判断momo.xlsx中是否存在,存在跳过,不存在添加到momo.xlsx中
#    (2)查询功能:查询英语单词的释义
#    (3)中文背诵:随机出现英文单词,只要填写一个中文释义正确就判断正确
#    (4)英文背诵:随机出现中文释义,输入英文单词
#    (5)背诵模式:显示要背诵的单词
###########################################################################################


import openpyxl
import random
import os


file_path = '***' #自己想要创建excel字典的路径
#判断词典是否创建
if os.path.exists(file_path):
    print ("momo词典已创建")
else :
    wb = openpyxl.Workbook()
    ws = wb.active
    ws.append(['单词','释义'])
    wb.save(file_path)
#打开excel
wb = openpyxl.load_workbook(file_path)
#选择工作表
sheet = wb['Sheet']
#获取字典最大行数
max_row = sheet.max_row
#选择行数
r =  int(input("请输行号(每50行一组):"))
#每天背诵个数
max_words = r+50
if max_words >max_row:
    max_words = max_row
words_number = max_words - r
words = [(row[0],row[1]) for row in sheet.iter_rows(min_row=r,max_row = max_words,values_only = True)] #将一维列表转换为二维列表
#随机不重复生成
random_word = random.sample(words,words_number)
a = 0
#添加单词
def add_word():
    
    word = input("请输入要添加的英文单词: ")
    #sheet激活
    #sheet = wb.active
    #遍历工作表
    for row in sheet.iter_rows(min_row=2,values_only=True): #从第二行开始遍历
        if row[0] == word:
            print ("该单词已存在")
            return
    meaning = input("请输入添加单词的中文释义(多义';'隔开):")
    #用户添加新单词
    sheet.append([word,meaning])
    wb.save(file_path)
    print ("单词添加成功")
#查询功能
def search_word():
    #sheet = wb.active
    word = input("请输入你要查询的单词:")
    i = 0 #判断单词存在的标志,0表示存在,1表示不存在
    for row in sheet.iter_rows(min_row=2,values_only=True):
        if word  in row:
            i =1
            print (row)
    if i ==0:
        print("字典没有要查询的单词,想要添加字典输入'1'")
#背诵功能
def recite_word():
    for row in sheet.iter_rows(min_row=r,max_row=max_words,values_only=True):
        print(row)
#默写中文
def chinese_recitation():
    #sheet = wb.active
    global a #将变量a声明为全局变量
    print ("*****英文:",random_word[a][0])
    #拆分中文释义
    chinese_words = random_word[a][1].split(';')
    #给予用户4次机会
    i=0
    while i != 3:
        answer = input("请输入中文释义:")    
        if answer in  chinese_words:
            print ("回答正确")
            break
        else:
            print ("不好意思回答错误,你剩余机会次数:", 2-i)
            i+=1
    if i == 3:
        print ("正确答案为:",chinese_words)
    a +=1
#默写英文
def english_recitation():
    global a #将变量a声明为全局变量
    print ("*****中文释义:",random_word[a][1]) #从一维列表选择中文释义
    #给予用户3次机会
    i = 0
    while i != 3:
        answer = input("请输入英文单词:")
        if answer == random_word[a][0]:
            print ("回答正确")
            break
        if answer != random_word[a][0]:
            print ("不好意思回答错误,你剩余机会次数:", 2-i)
            i+=1
    if i == 3:
        print ("正确答案为:",random_word[a])
    a +=1
#三种模式选择                
while True:
    print ('*'*40)
    print ("欢迎使用momo词典")
    print ("1.填充模式")
    print ("2.中文背诵")
    print ("3.英文背诵")
    print ("4.查询模式")
    print ("5.背诵模式")
    print ('*'*40)
    choice = input("请输入选项(输入q退出):")
    
    if choice == '1':
        add_word()
    elif choice == '2':
        chinese_recitation()
    elif choice == 'q':
        break
    elif choice == '3':
        english_recitation()
    elif choice == '4':
        search_word()
    elif choice == '5':
        recite_word()
    else :
        print ("输入选项无效")
print ("感谢使用momo词典")
 

本篇文章的程序功能可能还不完善,主要是记录自己初学python的成长。

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值