使用时有问题随机抽样在字典上

2024-09-30 18:13:13 发布

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

我正在为我的一位老师做一个测验来教她的学生。测验最后将有50多个问题。我发现随机抽样并在我的代码中实现了它,但似乎没有效果。即使我在使用随机抽样有时,这个问题会在刚接到电话后重复出现。我对Python还很陌生,这让我目瞪口呆。你知道吗

import random
# Stores Question, choices and answers 
questions = {
        'What should you do when links appear to be broken when using the Check Links Site wide command?':  # Q 1
         [' A) Test the link in a browser \n B) Test the link in live view \n C) Check the File path \n D) View Source Code', 'A'],

        'Which 3 combinations of factors encompass usability?':  # Q 2
        [' A) Amount of ads,Load time,Window Size \n B) Load Time,Ease of navigation,Efficiency of use \n C) Server download time, \n D) proper Navigation', 'B'],

        'Which line of html code describes a link to an absolute url using the <A> tag and href attribute?':  # Q 3
        [' A) <A herd = "http://www.acmetoon.org">Acme Toons!</a>, \n B) Herf = "http://www.acmetoon.org">Acme Toons!</a>,'
            '\n C) <A herf = "http://www.acmetoon.org">Acme Toons!</a> \n D) <A herf > = "http://www.acmetoon.org">Acme Toons!</a>', 'A']


        }


print('Dreamweaver Practice test V 1.0')


def pick_question():
        wrong_answers = 0
        while True:
            print()
            # Uses sample to get an item off the dict
            sample_question = random.sample(list(questions.keys()), 3) 
            # Converts the list to a single word ['hello'] -> hello 
            # So no errors complaining about it being it list popup
            new = sample_question[0]
            # Print question and choices 
            print(new)
            print(questions[new][0])
            print()
            user_answer = input('Enter Answer: ')
            print()
            # If the user choice matches the answer
            if user_answer == questions[new][1]:
                print('Correct')
                print()
                print('----Next Question----')
                print()
            elif wrong_answers == 10:
                print('Game Over')
                break
            else:
                print('Wrong')
                print('Correct letter was ' + questions[new][1])
                wrong_answers += 1
                print('Amount wrong ' + str(wrong_answers) + '/10')
                print()
                print('----Next Question----')
                print()


pick_question()

Tags: ofthetoorghttpnewwwwanswers
2条回答

以下内容可能有助于您了解代码真正在做什么,而不是为什么它的行为不符合预期:

import random
# Stores Question, choices and answers 
questions = {'key1': ['text1','ans1'], 'key2': ['text2','ans2'], 'key3':['text3','ans3']}

for i in range (0,10):
    sample_question = random.sample(list(questions.keys()), 3)
    print(sample_question)

一种可产生以下结果的样本输出:

['key2', 'key3', 'key1']
['key3', 'key1', 'key2']
['key3', 'key1', 'key2']
['key3', 'key1', 'key2']
['key1', 'key2', 'key3']
['key3', 'key2', 'key1']
['key1', 'key3', 'key2']
['key3', 'key2', 'key1']
['key1', 'key3', 'key2']
['key1', 'key2', 'key3']
['key3', 'key1', 'key2']

换言之,你实际上是在随机挑选一个问题,而不是从你的问题列表中删除这个问题。这就是为什么你有重复。这就像洗牌一副牌,指向其中一张,然后再洗牌,再指向其中一张-甚至没有从牌堆中移除任何东西。你知道吗

(我知道这并不能提供一个真正的“答案”,但您似乎想理解为什么您的代码不能提供答案表现得好。杰克的答案很好)

首先用random.shuffle随机化你的问题列表,然后像平常一样迭代:

...
def quick_questions():
    wrong_answers = 0
    question_keys = list(questions.keys())
    random.shuffle(question_keys) # questions is now in a random order
    for question_key in question_keys:
        new = questions[question_key]
        print()
        # Print question and choices 
        print(new)
        ...

相关问题 更多 >