Python-如何随机化具有A,B,C,D的问题

时间:2014-03-19 12:27:03

标签: python python-3.x random

我的目标是让它随意提问。

例如,测试开始,第一个问题可能是问题8.单词Question只是一个评论。

我希望它看起来像这样:

What does OSI stand for?

A- Open Systematic Information
B- Open Systems Interconnect
C- Organised Stairway Interweb
D- Open Safe Internet

以下是代码:

#Intro

name=input("Hello, what is your name? ")
print()
print ("Hello "+ name)
print()

valid = False
while not valid:
    ready=input("Are you ready to begin the test? (Please enter YES/NO)")
    print()

    if ready.lower() =="yes":
        print ("Excellent. Welcome to the Networking Principles test. ")
        valid = True
    elif ready.lower() =="no":
        print ("Okay, tell me when your ready. ")
    else:
        print ("Please asnwer yes or no")

count=0


if ready.lower()=="yes":
    print()
    print("Please answer each answer with A,B,C or D only. The test will now begin..."
    )


#Question 1
    print()
    print('What does OSI stand for?')
    print()
    print("A- Open Systematic Information")
    print("B- Open Systems Interconnect")
    print("C- Organised Stairway Interweb")
    print("D- Open Safe Internet")
    answer = input()

    if answer.lower() =="b":
        print ("Correct, Well Done")
        count = count + 1
    else:
        print ("Wrong Answer. The asnwer was B, OSI stands for Open Systems Interconnect")


#Question 2
    print()
    print("What is the fourth Layer of the OSI Model?")
    print()
    print("A- Transport Layer")
    print("B- Teleport Layer")
    print("C- Telecommunications Layer")
    print("D- Topology Layer")
    answer = input()

    if answer.lower() =="a":
        print ("Correct, Well Done")
        count = count + 1
    else:
        print ("Wrong Answer. Layer 4 is the Transport Layer")

4 个答案:

答案 0 :(得分:2)

您可以将答案放入列表中并在其上调用random.shuffle()

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

每次运行它时,它可能产生不同的输出,例如:

A- Organised Stairway Interweb
B- Open Systematic Information
C- Open Safe Internet
D- Open Systems Interconnect

答案 1 :(得分:2)

您可以将所有问题都保存在词典列表中:

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']}, 
             ...]

现在,您可以每次向用户提供一定数量的随机选择的问题:

import random
import string

to_answer = random.sample(questions, number_of_questions)

然后问这个问题:

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

并以随机顺序显示答案,将每个答案存储在a中的相应密钥(bcanswer_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

取用户的输入:

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

最后检查它们是否正确并报告回来:

    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)

这支持单个问题的多个正确答案的可能性,并且尽可能少地编码硬代码,因此整个事情由questions配置。这减少了代码的重复,并使以后更容易发现错误。

示例输出(对于number_of_questions = 1questions,如上所示):

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

答案 2 :(得分:1)

你可以简单地从文件中提取信息,

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:

答案 3 :(得分:-1)

姓名QuestionX。 然后从中选择一个或随机随机播放这些QuestionX

例如:

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

如果你想改变问题中的选择。 你可以在上面做同样的事情。

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

输出:

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

正如您在输出中看到的那样,您似乎不应该对选择的名称进行硬编码。你应该在改组后添加A,B,C,D。