循环选择题

Looping through multiple choice questions

我正在做一个测验来帮助我学习 a+ 认证,并帮助我在 Python 上工作。我想做的是循环选择题,同时保持我的代码干净,不重复我自己。我已经编写了几个问题,但我想要清理它的建议。我已经看过了interwebs 并找到关于这个特定事物的缺乏信息。我希望能够遍历问题而不必调用底部的每个问题方法。感谢您的帮助。这是我的代码:

answer = 0
correct = ""


def getAnswer():

    print "\n"
    prompt = ">"
    rawAnswer = raw_input(prompt)
    userAnswer = rawAnswer.upper()
    print "\n"

    if userAnswer == correct:
        print "That is correct!!! \n\n"
        print "***************************************************************\n"
        global answer
        answer += 1
    else:
        print "That was wrong"


def clrscreen():
    print ("\n" * 100)


def question1():
        print """Which of the following connector types is used by fiber-optic cabling?"

          Select the correct answer:

          A. LC
          B. RJ45
          C. RG-6
          D. RJ11"""
    enter code here
        global correct
        correct = "A"
        getAnswer()


def question2():
        print """Which protocol uses port 53?

          Select the correct answer:

          A. FTP
          B. SMTP
          C. DNS
          D. HTTP """

        global correct
        correct = "C"
        getAnswer()


def question3():
        print """You are making your own network patch cable. You need to attach an RJ45 plug to the end of a twisted-pair cable. Which tool should you use?

          Select the correct answer:

          A. Tone and probe kit
          B. Cable tester
          C. Crimper
          D. Multimeter"""

        global correct
        correct = "C"
        getAnswer()

def question4():
        print """Which port number does HTTP use?

          Select the correct answer:

          A. 21
          B. 25
          C. 80
          D. 110"""

        global correct
        correct = "C"
        getAnswer()

def question5():
        print """What device connects multiple computers together in a LAN?

          Select the correct answer:

          A. Modem
          B. Router
          C. Switch
          D. Firewall"""

        global correct
        correct = "C"
        getAnswer()

def question6():
        print """What is the name of a wireless network referred to as?

          Select the correct answer:

          A. SSID
          B. WPA
          C. DMZ
          D. DHCP"""

        global correct
        correct = "A"
        getAnswer()

question1()
clrscreen()
question2()
clrscreen()
question3()
clrscreen()
question4()
clrscreen()
question5()
clrscreen()
question6()
clrscreen()


score = round(answer/6.0 * 100)
print "Your score was ", score, "%"
print "\n"

您应该使用列表或字典,而不是针对到达问题的函数。考虑以下因素:

questions = [ {prompt, [list of choices], correct answer index}, ... ]

所以一个例子可以是:

questions = [ {'prompt':"What is 2 + 2?", 'choices': [3, 4, 5], 'answer_index': 1},
              {'prompt':"What is 2 + 3?", 'choices': [3, 4, 5, 6, 7], 'answer_index': 2} ]

然后你可以像这样浏览它们:

for q in questions:
    print(q['prompt'])
    for i, c in enumerate(q['choices']):
        print(chr(97 + i) + ':', c)
    response = input("enter your answer:\n>>> ")
    print('The correct answer is:', chr(97 + q['answer_index']), '\n\n\n')

当我 运行 那个(并输入答案)时,我得到:

What is 2 + 2?
a: 3
b: 4
c: 5
enter your answer:
>>> b
The correct answer is: b 



What is 2 + 3?
a: 3
b: 4
c: 5
d: 6
e: 7
enter your answer:
>>> c
The correct answer is: c