如何为一组可伸缩的定义/术语对建模?

2024-09-28 22:30:21 发布

您现在位置:Python中文网/ 问答频道 /正文

现在我的抽认卡游戏使用的是prepvocab()方法

  • 把一周的术语和翻译定义为一本字典
  • 添加对该周条款的描述
  • 把它们放进一个字典列表,用户在其中选择“周”来学习

每次我添加一个新的一周的术语和翻译,我都会在可用词典列表中添加另一个元素。我可以肯定这不是一件好事。你知道吗

class Vocab(object):

def __init__(self):
    vocab = {}
    self.new_vocab = vocab
    self.prepvocab()

def prepvocab(self):
    week01 = {"term":"translation"} #and many more...
    week01d = "Simple Latvian words"

    week02 = {"term":"translation"}
    week02d = "Simple Latvian colors"

    week03 = {"I need to add this":"to self.selvocab below"}
    week03d = "Body parts"

    self.selvocab = [week01, week02] #, week03, weekn]
    self.descs = [week01d, week02d] #, week03, weekn]
    Vocab.selvocab(self)

def selvocab(self):
    """I like this because as long as I maintain self.selvocab,
    the for loop cycles through the options just fine"""
    for x in range(self.selvocab):
        YN = input("Would you like to add week " \
                   + repr(x + 1) + " vocab? (y or n) \n" \
                   "Description: " + self.descs[x] + " ").lower()
        if YN in "yes":
            self.new_vocab.update(self.selvocab[x])
    self.makevocab()

我可以肯定地看到,这将是一个痛苦的20+是不是的问题。我现在正在读咒语,并且正在考虑一次打印所有的描述,让用户选择他们想学习的所有内容。你知道吗

如何更好地维护代码的这一部分?有没有人有一个根本性的改革不是那么…程序性的?你知道吗


Tags: to用户self列表new字典def术语
2条回答

你应该把你的钱存起来术语:翻译对以及文本文件中的描述。然后,您的程序应该解析文本文件并发现所有可用的课程。这将允许您扩展可用的课程集,而无需编辑任何代码。你知道吗

对于课程的选择,编写一个print_lesson_choices函数,向用户显示可用的课程和描述,然后在选择课程时询问用户的输入。与其每节课都问他们一个问题,为什么不做如下提示:

self.selected_weeks = []

def selvocab(self):
    self.print_lesson_choices()
    selection = input("Select a lesson number or leave blank if done selecting: ")
    if selection == "": #Done selecting
        self.makevocab()
    elif selection in self.available_lessons:
        if selection not in self.selected_weeks:
            self.selected_weeks.append(selection)
            print "Added lesson %s"%selection
        self.selvocab() #Display the list of options so the user can select again
    else:
        print "Bad selection, try again."
        self.selvocab()

Pickling objects into a database意味着创建一个界面来修改前端的每周课程需要一些努力,但这是值得的。你知道吗

相关问题 更多 >