单词记忆系统一:菜单循环和菜单确认(Python 自定义类、自定义模块的相对路径import)


Python 官网https://www.python.org/




  自学并不是什么神秘的东西,一个人一辈子自学的时间总是比在学校学习的时间长,没有老师的时候总是比有老师的时候多。
            —— 华罗庚


等风来,不如追风去……



单词记忆系统一:
菜单循环和菜单确认
(Python 自定义类、自定义模块的相对路径import)




        目录


    1、本练习缘来
    2、“框架”构想
    3、系统主菜单
    4、菜单确认

回首页

本练习缘来

  想写这个“单词记忆系统”,是缘于看到有人想用“网”到的代码(点击蓝色文字查看那段代码)给孩子做一个“单词记忆”程序。仔细研读那段代码,感觉不是我想要的那种,决定给自己“鼓捣”一个“真正”的“单词记忆系统”,至少要让单词库得以在磁盘文件保存保存,才算得上勉强可用的“系统”。


“框架”构想

  我现在正在学习python class 和 python 的包、模块的 import ,正好在做这个“单词记忆系统”时炼炼。
  虽然代码有些丑,但终究还是通过了调试,达成目的。🤪🤪

在这里插入图片描述

系统目录结构

在这里插入图片描述

系统主程序代码

#!/sur/bin/nve python 
# coding: utf-8 

'''

filename: word_remember.py

author: 梦幻精灵_cq

time: 2022-6-19 10:31am

'''

from word_remember_tools import menu_show, ismenu
from mypytools.python_color import color

menu_str = '单词练习,单词测试,生词添加,系统文档'.split(',')

while True:
    menu_show.MenuShow().show(menu_str) # 调用模块列印菜单。
    try:
        menu_choice = int(input(f"{'-'*50}\n\n{'':>12}{color('选择菜单', 'f_green')}:"))
    except Exception as error:
        input(f"\n\n{'-'*50}\n{color('选择错误!'.center(45, '-'),'f_red')}\n{color('ErrorType:', 'f_gray')} {color(error, 'f_red')}\n{'-'*50}\n\n")
        continue
    if menu_choice not in range(len(menu_str)+1):
        input(f"\n\n{'-'*50}\n{color('选择错误!'.center(45, '-'),'f_red')}\n{'-'*50}\n\n")
        continue
    if not ismenu.IsMenu().ismenu(menu_choice):
        break # 菜单选择0,退出系统菜单循环。


回首页

系统主菜单


1. 菜单列印代码

#!/sur/bin/nve python 
# coding: utf-8 

'''

filename = 'word_remember_tools/menu_show.py'

author = 'Dream_elf'

time = '2022-06-19'

'''


from mypytools.mypythontools import localtime_show # 加载时间字符格式化字符串获取工具。
from mypytools.python_color import color # 加载打印色彩控制工具。
from os import system # 加载命令执行“容器”。

# 菜单列印模块。

class MenuShow:


    def show(self, menu_str):
        '''
        菜单列印。
        遍历菜单列表打印菜单。
        
        '''
        system('clear')
        print(f"\n\n{'*'*50}\n\n{'单词记忆系统'.center(44)}\n{color('(Word Remember)','f_green').center(59)}\n\n{localtime_show().center(61)}\n{'-'*50}")
        for k,i in enumerate(menu_str):
            print(f"\n{'':>20}{k+1}. {color(i, 'f_green')}")
        print(f"\n{'':>20}0. {color('退出系统','f_gray')}")

2. 菜单列印效果
在这里插入图片描述


回首页

菜单确认代码

#!/sur/bin/nve python 
# coding: utf-8 

'''

filename = 'word_remember_tools/ismenu.py'

author = 'Dream_elf'

time = '2022-06-19'

'''

from mypytools.python_color import color # 加载打印色彩控制工具。
from os import system # 加载命令执行“容器”。


class IsMenu:
    
    
    def ismenu(self, menu_choice):
        '''
        菜单选择确认,根据所选序号,
        调用相应功能模块。
        '''
        num = menu_choice # 菜单选择变量别名。
        if not num: # 退出系统。
            print(f"\n\n{'-'*50}\n{color('正在保存词库……','f_green').center(51)}\n{'-'*50}")
            pass # 词库保存,程序设计中。
            system('clear')
            print(f"\n\n{color('-'*50, 'f_green')}\n\n{color('谢谢使用“单词记忆系统”!','f_yellow').center(48)}\n\n{color('Author: Dream Elf','f_gray').center(56)}\n{color('*'*50,'f_green')}\n\n")
            return num
        elif num == 1: # 单词练习。
            input(f"\n\n{'-'*50}\n{color('单词练习正在建设……','f_green').center(49)}\n{'-'*50}")
            return num
        elif num == 2: # 单词测试。
            input(f"\n\n{'-'*50}\n{color('单词测试正在建设……','f_green').center(49)}\n{'-'*50}")
            return num
        elif num == 3: # 生词添加。
            input(f"\n\n{'-'*50}\n{color('生词添加正在建设……','f_green').center(49)}\n{'-'*50}")
            return num
        elif num == 4: # 说明文档。
            input(f"\n\n{'-'*50}\n{color('说明文档正在建设……','f_green').center(49)}\n{'-'*50}")
            return num 

菜单选择错误提示


1. 输入错误
在这里插入图片描述


2. 超出范围
在这里插入图片描述


优化了“菜单选择”错误提示字符串列印格式:

1. 代码

from word_remember_tools import menu_show, ismenu
from mypytools.python_color import color

menu_str = '单词练习,单词测试,生词添加,系统文档'.split(',') # 系统菜单字符串列表。

while True:
    menu_show.MenuShow().show(menu_str) # 调用模块列印菜单。
    try:
        menu_choice = int(input(f"{'-'*50}\n\n{'':>12}{color('选择菜单', 'f_green')}:"))
    except Exception as error:
        input(f"\n\n{'-'*50}\n\n{color(' 选择错误!'.center(45, '-'),'f_red')}\n\n{color('ErrorType:', 'f_gray')} {color(error, 'f_yellow')}\n{'-'*50}\n\n")
        continue
    if menu_choice not in range(len(menu_str)+1):
        error = f"您输入的数字,超出菜单序号选择范围 '1~{len(menu_str)}' :'{menu_choice}' "
        input(f"\n\n{'-'*50}\n\n{color('选择错误!'.center(45, '-'),'f_red')}\n\n{color('错误类型:', 'f_gray')}{color(error, 'f_yellow')}\n{'-'*50}\n\n")
        continue
    if not ismenu.IsMenu().ismenu(menu_choice):
        break # 菜单选择0,退出系统菜单循环。

2. 效果
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
(2022-06-20 06:29 am)


菜单确认之“系统退出”提示
在这里插入图片描述


回目录


别人“网”来,准备给孩子做“单词记忆”程序的代码:

import random
import turtle as t

WORDS = {"easy": "简单", "difficult": "困难", "answer": "答案"}

iscontinue = "y"
while iscontinue == "y" or iscontinue == "Y":

      print(
          """
                          欢迎使用BillChen单词速背系统 
             英译汉请输入Y    汉译英请输入N   添加单词请按L
             模拟练习请按T    结束程序请按W   开发详情请按任意键
"""
      )
      F = input()
      if F == 'N' or F == 'n':
            new_WORDS = {v: k for k, v in WORDS.items()}
            n = input("请输入需要查询的单词或词语:")
            if n in new_WORDS:
                  print(new_WORDS[n])
            else:
                  print('暂未收录,敬请期待')
            iscontinue = input("\n\n是否继续(Y/N):")
      elif F == 'Y' or F == 'y':
            n = input("请输入需要查询的单词或词语:")
            if n in WORDS:
                  print(WORDS[n])
            else:
                  print('暂未收录,敬请期待')
            iscontinue = input("\n\n是否继续(Y/N):")
      elif F == 'L' or F == 'l':
            new_value = input('请输入一个新的单词的释义:')
            new_key = input('请输入这个新单词:')
            WORDS[new_key] = new_value
            print(WORDS)
      elif F == 'T' or F == 't':
            i = 0
            z = 0
            while i < 5:

                  key = random.choice(list(WORDS))
                  right_key = WORDS[key]
                  print(key)
                  user_key = input("请输入这个单词的释义:")
                  if user_key == right_key:
                        print('恭喜您,此题答对了')

                        z = z + 1

                  else:
                        print('很遗憾,此题打错了,再接再厉哦')
                        print('正确答案是:{}'.format(right_key))
                        '''
                                       2021/2/7根据树扇风吹云起的提议增添答错时会输出正确答案
                                    '''

                  i = i + 1

            print('恭喜您,本次模拟结束,本次您的正确率为:{:.2%}'.format(z / 5))
      elif F == 'W' or F == 'w':
            print("程序已经退出,欢迎您的下次使用")
            iscontinue = "n"
      else:
            t.setup(1800, 800, 0, 0)
            t.bgcolor('pink')
            t.color('red')
            t.shape('turtle')
            t.speed(5)
            t.begin_fill()
            t.up()
            t.goto(-120, 100)
            t.down()
            for i in range(5):
                  t.forward(240)
                  t.right(144)
            t.end_fill()
            t.penup()
            t.goto(200, 100)
            t.pendown
            t.color('black')
            t.write('开发人员:popolo', font=("Arial", 34, "normal"))
            t.right(90)
            t.fd(100)
            t.color('red')
            t.write('♥', font=("Arial", 34, "italic"))
            t.left(90)
            t.fd(50)
            t.color('black')
            t.write('学号:201805050118', font=("Arial", 34, "italic"))
            t.right(90)
            t.fd(100)
            t.write('班级:18计科本1班', font=("Arial", 34, "bold"))
            t.right(90)
            t.fd(500)
            t.write('考试必过', font=("Arial", 34, "bold"))
            t.right(90)
            t.fd(300)
            t.write('单词速背系统', font=("Arial", 34, "bold"))
            t.hideturtle()
            t.exitonclick()

回首页


My Up and Down
__上一篇:__ 练习:数字时钟(Python 自定义类)

__下一篇:__ 仿真模拟双色球

我的HOT博:
推荐条件 点阅破千

回目录


老齐漫画头像

精品文章:

来源:老齐教室


回目录

Python 入门指南【Python 3.6.3】

好文力荐:

CSDN实用技巧博文:


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦幻精灵_cq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值