Python 循环选择题

Python 循环选择题,python,choice,Python,Choice,我正在做一个测验,帮助我学习a+认证,并帮助我学习Python。我“我试着做的是循环选择题,同时保持代码整洁,不重复我自己。我已经安排了几个问题,但我想听听关于清理的建议。我浏览了所有的互联网站,发现缺少关于这件事的信息。我希望能够循环通过问题,而不必调用底部的每个问题方法。谢谢你的帮助。这是我的密码: answer = 0 correct = "" def getAnswer(): print "\n" prompt = ">" rawAnswer = ra

我正在做一个测验,帮助我学习a+认证,并帮助我学习Python。我“我试着做的是循环选择题,同时保持代码整洁,不重复我自己。我已经安排了几个问题,但我想听听关于清理的建议。我浏览了所有的互联网站,发现缺少关于这件事的信息。我希望能够循环通过问题,而不必调用底部的每个问题方法。谢谢你的帮助。这是我的密码:

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"

您应该使用列表或字典,而不是reach问题的函数。考虑以下事项:

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')
当我运行它(并输入答案)时,我得到:


就评分系统和if函数而言,这是否也适用于检查给定答案是否正确?您需要做的就是询问他们的答案,存储它,并将其与正确答案进行比较。如果分数对你很重要,你可以考虑存储答案索引,而不是答案,然后用索引打印出选择。这样做会使检查答案变得微不足道。感谢您的快速回复。我还希望能够键入a、b、c或d作为答案,而不是字符串。我已经找了一段时间了,但是我找不到如何正确地将答案设置为变量而不是字典中的字符串。我编辑了答案以添加字母索引。如果需要大写字母,请将97改为65。然后,如果您需要跟踪分数,只需添加一个新变量,并将正确答案与输入答案进行比较。
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