我正在做一个小测验来帮助我学习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问题的函数。考虑以下因素:
例如:
^{pr2}$然后你就可以像这样把它们都看一遍:
当我运行(并输入答案)时,我得到:
相关问题 更多 >
编程相关推荐