循环选择多项问题

时间:2015-10-11 19:31:21

标签: python choice

我正在进行一项测验,帮助我学习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"

1 个答案:

答案 0 :(得分:2)

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

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