首页 文章

for循环Python 3.5 . 为我的导师创建测验

提问于
浏览
1

您好我正在尝试为年级英语学生做一个测验申请 . 我正在使用Python 3.5 . 而且我有点新鲜 . quizz应该像下面这样工作我也会包含代码 .

欢迎来到你的测验单词1/5:马铃薯这个词含有多少个附属品? 3正确! Word 2/5:Potato这个词含有多少个元音? 1正确! Word 3/5:名称该单词包含多少个元音5不正确!正确答案4 Word 4/5:是这个词含有多少个字母? 3正确! Word 5/5:Day这个词的第3个字是什么? Y正确!游戏结束 . 你的分数是4/5

print('WELCOME TO YOUR QUIZ')

# Import the random module to allow us to select the word list and questions at random.
import random
quizWords = ['WOMBAT', 'COMPUTER', 'BOOKS', 'DAY', 'NIGHT', 'HOUR', 'POTATO', 'HUGE', 'TINY', 'GOOD', 'BAD', 'YES', 'NO', 'HOUSE', 'QUESTION', 'BALLOON', 'CAT', 'DUCK', 'KOOKARBURRA', 'POSTER', 'TELEVISION', 'PRINCE', 'RHYTHM', 'SUDDENLY', 'SNOW', 'MAGNET', 'TOWEL', 'WALKING', 'SPEAKER', 'UNCHARACTERISTICALLY']
s = random.sample (quizWords, 5)
# loop through the list using enumerate 
for index,w in enumerate(s):
    print("Word {}/{}:{}".format(index+1,len(s),w))

我想生成1到4之间的随机数,并使用它来选择每个单词要求的问题 .

如果随机数为1,我想询问用户“该单词包含多少个字母?”并提示输入 . 评估他们的答案,然后打印相应的消息 . 我怎么会适合这个?

3 回答

  • 0

    这就是我如何创建测验 . 显然,您可以更新print语句以遵循您自己想要的格式 . 希望这有帮助!

    import random
    import string
    
    def consonant_count(word):
        word = word.lower()
        return len([x for x in word if x in consonants])
    
    def vowel_count(word):
        word = word.lower()
        return len([x for x in word if x in vowels])
    
    def prompt_letter_count(word):
        correct = word_map[word]['letters']
        ans = input('How many letters does "{}" contain?'.format(word))
        return check(ans, correct)
    
    def prompt_vowel_count(word):
        correct = word_map[word]['vowels']
        ans = input('How many vowels does "{}" contain?'.format(word))
        return check(ans, correct)
    
    def prompt_consonant_count(word):
        correct = word_map[word]['consonants']
        ans = input('How many consonants does "{}" contain?'.format(word))
        return check(ans, correct)
    
    def prompt_random_letter(word):
        n = random.randint(0, len(word))
        correct = word[n-1]
        ans = raw_input('What is letter {} of "{}"?'.format(n, word))
        return check(ans.lower(), correct.lower())
    
    def check(ans, correct):
        if ans == correct:
            return prompt_correct()
        return prompt_incorrect()
    
    
    def prompt_correct():
        print('That is correct! :)')
        return 1
    
    def prompt_incorrect():
        print('That is incorrect :(')
        return 0
    
    def next_question(word):
        q_type = input_map[random.randint(1, 4)]
        return q_type(word)
    
    vowels = ['a', 'e', 'i', 'o', 'u']
    consonants = [x for x in string.ascii_lowercase if x not in vowels]
    quizWords = ['WOMBAT', 'COMPUTER', 'BOOKS', 'DAY', 'NIGHT', 'HOUR', 'POTATO', 'HUGE', 'TINY', 'GOOD', 'BAD', 'YES', 'NO', 'HOUSE', 'QUESTION', 'BALLOON', 'CAT', 'DUCK', 'KOOKARBURRA', 'POSTER', 'TELEVISION', 'PRINCE', 'RHYTHM', 'SUDDENLY', 'SNOW', 'MAGNET', 'TOWEL', 'WALKING', 'SPEAKER', 'UNCHARACTERISTICALLY']
    word_map = {x:{'consonants':consonant_count(x), 'vowels':vowel_count(x), 'letters':len(x)} for x in quizWords}
    input_map = {1:prompt_letter_count, 2:prompt_vowel_count, 3:prompt_consonant_count, 4:prompt_random_letter}
    
    def start_quiz(number_questions):
        current_question = 0
        correct_questions = 0
        if number_questions > len(quizWords):
            number_questions = len(quizWords)
        sample_questions = random.sample(quizWords, number_questions)
        print('WELCOME TO YOUR QUIZ')
        print '---------------------'
        for x in sample_questions:
            print 'Question {}/{}:'.format(current_question, number_questions)
            correct_questions += next_question(x)
            print '---------------------'
            current_question += 1
        print 'Congragulations on completing your quiz!'
        print "    Score {}/{}:".format(correct_questions, number_questions)
        try_again = raw_input('Would you like to try again? (y/n)').lower()
        if try_again == 'y' or try_again == 'yes':
            start_quiz(number_questions)
    
    start_quiz(4)
    
  • 1

    我提出了一种选择问题的方法,并将测试函数关联起来,这样可以轻松定义您想要的任何问题:

    print('WELCOME TO YOUR QUIZ')
    
    # Import the random module to allow us to select the word list and questions at random.
    import random
    quizWords = ['WOMBAT', 'COMPUTER', 'BOOKS', 'DAY', 'NIGHT', 'HOUR', 'POTATO', 'HUGE', 'TINY', 'GOOD', 'BAD', 'YES', 'NO', 'HOUSE', 'QUESTION', 'BALLOON', 'CAT', 'DUCK', 'KOOKARBURRA', 'POSTER', 'TELEVISION', 'PRINCE', 'RHYTHM', 'SUDDENLY', 'SNOW', 'MAGNET', 'TOWEL', 'WALKING', 'SPEAKER', 'UNCHARACTERISTICALLY']
    s = random.sample (quizWords, 5)
    
    def test_nb_vows(word):
        return sum(word.upper().count(x) for x in "AEIOUY")
    
    
    qt = {"How many vowels does the word contain?" : test_nb_vows, "How many letters does the word contain?" : len}
    
    # loop through the list using enumerate
    for index,w in enumerate(s):
        print("Word {}/{}:{}".format(index+1,len(s),w))
        # check question
        qk = random.choice(list(qt.keys()))
        answer = int(input(qk))
        real_answer = qt[qk](w)
        if real_answer == answer:
            print("Correct!")
        else:
            print("Noooo it is {}".format(real_answer))
    

    我们的想法是使用 random.choice 随机选择问题 . 选择是字典的关键,值是测试函数,它必须有1个参数:单词本身,并返回正确的答案(现在仅限于一个数字),这是针对用户输入进行测试的

    这样,只要您提供相关的测试功能,就可以定义很多问题 . 你只需要丰富 qt 并对测试函数进行编码(或者像我通过重用 len 那样使用现有的函数) . 祝好运 .

  • 0

    这是一个可能的解决方案:

    # Import the random module to allow us to select the word list and
    # questions at random.
    import random
    
    quizWords = [
        'WOMBAT', 'COMPUTER', 'BOOKS', 'DAY', 'NIGHT', 'HOUR', 'POTATO', 'HUGE',
        'TINY', 'GOOD', 'BAD', 'YES', 'NO', 'HOUSE', 'QUESTION', 'BALLOON',
        'CAT', 'DUCK', 'KOOKARBURRA', 'POSTER', 'TELEVISION', 'PRINCE', 'RHYTHM',
        'SUDDENLY', 'SNOW', 'MAGNET', 'TOWEL', 'WALKING', 'SPEAKER',
        'UNCHARACTERISTICALLY'
    ]
    s = random.sample(quizWords, 5)
    
    def f0(word):
        """ TODO """
        return True
    
    def f1(word):
        """ TODO """
        return True
    
    def f2(word):
        """ TODO """
        return True
    
    def f3(word):
        """ TODO """
        return True
    
    def f4(word):
        """ TODO """
        return True
    
    questions = {
        0: {
            "title": "How many consanants does the word {0} contain?",
            "f": f0
        },
        1: {
            "title": "How many vowels does the word {0} contain?",
            "f": f1
        },
        2: {
            "title": "How many vowels does the word {0} contain",
            "f": f2
        },
        3: {
            "title": "How many letters does the word {0} contain?",
            "f": f3
        },
        4: {
            "title": "What is letter 3 of the word {0}?",
            "f": f4
        }
    }
    
    
    # loop through the list using enumerate
    for index, w in enumerate(s):
        print("Word {}/{}:{}".format(index + 1, len(s), w))
        type_question = random.randint(0, 4)
        print("{}".format(questions[type_question]["title"].format(w)))
        questions[type_question]["f"](w)
    

    如您所见,您只需使用random.randint从问题词典中提取随机问题 . 现在你只需要完成函数f0..f4 ;-)的逻辑 . 希望能帮助到你!

相关问题