mercredi 7 décembre 2016

Having problems using random.sample on a dictionary

I'm making a quiz for a teacher of mine to teach her students. The quiz is going to have 50+ questions at the end. I found about random.sample and implemented it in my code but it seems to have not effect. Even though I'm using random.sample sometimes the question repeat themselves after just being called. I'm pretty new to Python and I'm dumbfounded by this.

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




Aucun commentaire:

Enregistrer un commentaire