Python- How can I randomise questions that have an A,B,C,D

后端 未结 4 602
旧时难觅i
旧时难觅i 2020-12-22 13:40

My aim is to have it so it can randomise questions.

For example, the test starts and the first question could be question 8. The word Question is only

相关标签:
4条回答
  • 2020-12-22 14:00

    You could hold all of your questions in a list of dictionaries:

    questions = [{'question': 'What does OSI stand for?',
                  'correct': ['Open Systems Interconnect'],
                  'incorrect': ['Open Systematic Information', 
                                'Organised Stairway Interweb',
                                'Open Safe Internet']},
                 {'question': "What is the fourth Layer of the OSI Model?",
                  'correct': ['Transport Layer'],
                  'incorrect': ['Teleport Layer', 
                                'Telecommunications Layer', 
                                'Topology Layer']}, 
                 ...]
    

    Now you can give the user a given number of randomly-selected questions each time:

    import random
    import string
    
    to_answer = random.sample(questions, number_of_questions)
    

    And then ask the question:

    for q_num, question in enumerate(to_answer, 1):
        print("Question {0}: {1}".format(q_num, question['question']))
    

    and present the answers in a random order, storing each against the corresponding key (a, b, c, etc.) in answer_key:

        answers = question['incorrect'] + question['correct']
        random.shuffle(answers)
        answer_key = {}
        for answer, key in zip(answers, string.ascii_lowercase):
            print("{0}: {1}".format(key, answer))
            answer_key[key] = answer
    

    Take the user's input:

        while True:
            user_answer = input().lower()
            if user_answer not in answer_key:
                print("Not a valid answer")
            else:
                break
    

    And finally check whether they're correct and report back:

        correct = question['correct']
        if answer_key[user_answer] in correct:
            print("Correct!")
        else:
            s = "Incorrect; the correct answer{0}:"
            print(s.format(" was" if len(correct) == 1 else "s were"))
            for answer in correct:
                print(answer)
    

    This supports the possibility of multiple correct answers for a single question, and hard-codes as little as possible so the whole thing is configured by questions. That reduces repetition of code, and makes it easier to find bugs later on.

    Example output (for number_of_questions = 1 and questions as shown above):

    Question 1: What does OSI stand for?
    a: Open Systematic Information
    b: Open Safe Internet
    c: Organised Stairway Interweb
    d: Open Systems Interconnect
    e
    Not a valid answer
    b
    Incorrect; the correct answer was:
    Open Systems Interconnect
    
    0 讨论(0)
  • 2020-12-22 14:10

    you can do simple one pulling information from files like so,

    while count < 10:
    wordnum = random.randint(0, len(questionsfile)-1)
    print 'What is:  ', answersfile[wordnum], ''
    options = [random.randint(0, len(F2c)-1),
        random.randint(0, len(answersfile)-1),random.randint(0, len(answersfile)-1)]
    options[random.randint(0, 2)] = wordnum
    print '1 -', answersfile[options[0]],
    print '2 -', answersfile[options[1]],
    print '3 -', answersfile[options[2]],
    print '4 -', answersfile[options[3]]
    answer = input('\nYou  choose number ?: ')
    if options[answer-1] == wordnum:
    
    0 讨论(0)
  • 2020-12-22 14:11

    Name QuestionX. Then choose one from or shuffle these QuestionX randomly.

    For example:

    import random
    def Question1():
        print('Q1:What does OSI stand for?')
        answer = raw_input()
        print(answer)
    
    def Question2():
        print("Q2:What is the fourth Layer of the OSI Model?")
        answer = raw_input()
        print(answer)
    
    Questions = [Question1, Question2]
    
    #Solution 1
    q = random.choice(Questions)
    q()
    
    #Solution 2
    for q in random.sample(Questions, len(Questions)):
        q()
    
    #Solution 3
    random.shuffle(Questions)
    for q in Questions:
        q()
    

    If you want to shuffle choices in the question. You can do same above.

    def Question1():
        print('What does OSI stand for?')
        def A(): print("A- Open Systematic Information")
        def B(): print("B- Open Systems Interconnect")
        def C(): print("C- Organised Stairway Interweb")
        def D(): print("D- Open Safe Internet")
        choices = [A, B, C, D]
        random.shuffle(choices)
        for c in choices:
            c()
    
    Question1()
    

    Output:

    What does OSI stand for?
    B- Open Systems Interconnect
    D- Open Safe Internet
    C- Organised Stairway Interweb
    A- Open Systematic Information
    

    As you can see in the output, it seems you shouldn't hard-code the name of choices. You should add A,B,C,D after shuffling.

    0 讨论(0)
  • 2020-12-22 14:12

    You could put answers in a list and call random.shuffle() on it:

    import random
    
    answers = [
        "Open Systematic Information",
        "Open Systems Interconnect",
        "Organised Stairway Interweb",
        "Open Safe Internet",
    ]
    random.shuffle(answers)
    
    for letter, answer in zip("ABCD", answers):
        print("{}- {}".format(letter, answer))
    

    Each time you run it, it may produce different output e.g.:

    A- Organised Stairway Interweb
    B- Open Systematic Information
    C- Open Safe Internet
    D- Open Systems Interconnect
    
    0 讨论(0)
提交回复
热议问题