进行测验建设'与数组

时间:2014-01-08 19:43:42

标签: python

我正在构建一个简单的“测验计划”。代码在这里:

import random

wordList1 = []
wordList2 = []

def wordAdd():
    wordNew1 = str(input("Add a word to your wordlist: "))
    wordNew2 = str(input("Add the translation to this word: "))
    if wordNew1 != "exit":
        wordList1.append(wordNew1)
        wordAdd()
    elif wordNew2 != "exit":
        wordList2.append(wordNew2)
        wordAdd()
    else:
        exercise()

def exercise():
    q = random.choice(wordList1)
    a = wordList2
    if q[] == a[]:
        print("Correct!")
    else:
        print("Wrong!")

wordAdd()

我正在尝试检查wordList1-number并将其与wordList2-number进行比较。 现在我没想到def运动会起作用,但我找不到让它运转的解决方案...... 我知道Python中的字典 - 但我想知道在Python中这样的数组结构是否可行。

有人可以帮我这个吗?

提前致谢! Sytze

1 个答案:

答案 0 :(得分:0)

我玩了一点你的代码。我不确定我是否完全理解你的问题,但我让它按照我认为需要工作的方式工作。我添加了一些评论,以明确我做了什么。

我试图坚持你的基本概念(递归除外),但我重命名了许多东西以使代码更具可读性。

import random

words = []
translations = []

def add_words():
    # input word pairs until the user inputs "exit"
    print('\nInput word and translation pairs. Type "exit" to finish.')
    done = False
    while not done:
        word = raw_input("Add a word to your wordlist: ")
        # only input translation, if the word wasn't exit
        if word != "exit":
            translation = raw_input("Add the translation to this word: ")
        if word != "exit" and translation != "exit":
            # append in pairs only
            words.append(word)
            translations.append(translation)
        else:
            done = True

def exercise():
    # excercising until the user inputs "exit"
    print("\nExcercising starts. ")
    done = False
    while not done:
        # get a random index in the words
        index = random.randrange(0, len(words))
        # get the word and translation for the index
        word = words[index]
        translation = translations[index]
        # ask the user
        answer = raw_input('Enter the translation for "%s": ' % word)
        if answer == "exit":
            done = True
            print("\nGoodbye!")
        elif answer == translation:
            print("Correct!")
        else:
            print("Wrong!")

add_words()
exercise()