2016-02-04 49 views
2

我有两种语言的单词列表,其中每个外语单词(在下一行中)按其已知语言的含义进行跟踪。例如:使用队列的词汇测试

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)的含义将成为下一个单词(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 == [] 

回答

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() 
+0

我已经创建了我自己的班级称为队列(检查我在OP编辑)。你可以在代码中定义属性队列吗?我可能会在我的课堂上忽略它。 – Lozansky

+0

@Lozansky我使用Python3提供的bulit-in队列模块,你可以在这里查看:https://docs.python.org/3/library/queue.html#queue-objects – Dinever

+0

@Lozansky我修改了回答并添加了另一个脚本以适合您的问题。 – Dinever

-1

在这种情况下,我将只为每个单词使用list。我不认为你真的需要一个新班。我结束了程序是有点长(注意这是蟒蛇2):

#!/usr/bin/env python 

from Queue import Queue 

FILENAME='words.txt' 

def main(): 
    q1 = readwords() 
    q2 = Queue() 

    while (not q1.empty()) or (not q2.empty()): 
     if not q1.empty(): 
      src = 1 
      word = q1.get() 
     else: 
      src = 2 
      word = q2.get() 
     ans = input('What does "'+word[0]+'" mean? ') 
     if ans==word[1]: 
      print 'Correct!' 
      src += 1 
     else: 
      print 'Incorrect! "'+word[1]+'"' 
     if src==1: 
      q1.put(word) 
     elif src==2: 
      q2.put(word) 

    print 'Done! Good job!' 

def readwords(): 
    with open(FILENAME,'r') as f: 
     lines = f.readlines() 

    first = True 
    words = Queue() 
    word = [None,None] 
    for x in lines: 
     if not x.strip(): 
      continue 
     if first: 
      word[0] = x.strip() 
      first = False 
     else: 
      word[1] = x.strip() 
      first = True 
      words.put(word) 
      word = [None,None] 
    return words 

if __name__ == '__main__': 
    main()