使用Queue进行词汇测试

时间:2016-02-04 22:12:19

标签: python python-3.x python-3.3

我有一个包含两种语言单词的列表,其中每个外来单词都遵循其已知语言的含义(在下一行)。例如:

hablar
talk
llamar
call

现在我希望使用队列创建词汇表测试。我的想法是创建2个队列q1和q2。所有单词都从q1开始,直到每个单词的含义都被正确猜出。如果你得到一个错误的单词,这个单词将被放置在q1队列的末尾,如果你说得对,它将被移动到q2。当q1为空时,你移到q2并做同样的事情,除了被扔掉的字样'当你正确回答并且q2为空时测试完成。

问题是我无法弄清楚如何创建一种将外来词与已知单词相关联的方法。我想我应该这样开始:

class Wordpair():
    def __init__(self, l1, l2):
        self.lang1 = l1
        self.lang2 = l2

    def __str__(self):
        question=input('What does'+self.lang1+'mean?')

但我不知道如何测试preson是否正确回答。此外,我认为我可以以某种方式实现节点,因为单词(节点)的含义将是下一个单词(Node.next)。随着我的进步,我会更新,但我很感激你现在的任何提示 Edit1:这就是我创建队列的方式:

class Queue:
    def __init__(self):
        self.items = []
    def put(self, item):
        self.items.append(item)
    def get(self):
        return self.items.pop(0)      
    def isempty(self):
        return self.items == []

2 个答案:

答案 0 :(得分:1)

您可以简单地将用户输入与单词的已知含义进行比较。

我已经编写了一个简单的脚本(Python3)来解决这个问题,希望它有所帮助!

import queue


class Word(object):

    def __init__(self, foreign_meaning, known_meaning):
        self.foreign_meaning = foreign_meaning
        self.known_meaning = known_meaning


if __name__ == '__main__':
    q1 = queue.Queue()
    q2 = queue.Queue()

    # Put some sample words
    q1.put(Word('hablar', 'talk'))
    q1.put(Word('llamar', 'call'))

    while True:
        if q1.empty():
            print("You have finished all words!")
            break

        word = q1.get() # Get the next word in q1

        ans = input("What is the meaning of `{0}`? ".format(word.foreign_meaning)) # Fetch user input as the answer
        if ans == word.known_meaning:
            print("Correct!")
            q2.put(word)
        else:
            print("Wrong! The meaning of `{0}` is `{1}`".format(word.foreign_meaning, word.known_meaning))
            q1.put(word)
        print()

看起来您正在使用自己的队列实现。

我修改了我的代码以适应问题。但是,强烈建议您使用built-in queue module provided by Python3,因为它是线程安全的。

#!/usr/bin/env python


class Queue:

    def __init__(self):
        self.items = []

    def put(self, item):
        self.items.append(item)

    def get(self):
        return self.items.pop(0)

    def isempty(self):
        return self.items == []


class Word(object):

    def __init__(self, foreign_meaning, known_meaning):
        self.foreign_meaning = foreign_meaning
        self.known_meaning = known_meaning


if __name__ == '__main__':
    q1 = Queue()
    q2 = Queue()

    # Put some sample words
    q1.put(Word('hablar', 'talk'))
    q1.put(Word('llamar', 'call'))

    while True:
        if q1.isempty():
            print("You have finished all words!")
            break

        word = q1.get() # Get the next word in q1

        ans = input("What is the meaning of `{0}`? ".format(word.foreign_meaning)) # Fetch user input as the answer
        if ans == word.known_meaning:
            print("Correct!")
            q2.put(word)
        else:
            print("Wrong! The meaning of `{0}` is `{1}`".format(word.foreign_meaning, word.known_meaning))
            q1.put(word)
        print()

答案 1 :(得分:-1)

在这种情况下,我只会为每个单词使用extension String { func CGFloatValue() -> CGFloat? { guard let doubleValue = Double(self) else { return nil } return CGFloat(doubleValue) } } 。我不认为你真的需要一个新的课程。我最终得到的程序有点长(注意这是python 2):

CGFloat?
相关问题