我开发了一个测试,我想让用户在测试开始之前输入自己的答案。这是我到目前为止的情况。如果我使用字典而不是多次进行原始输入会更好,还是有一种方法可以直接向用户询问答案而不必这样做。谢谢!在
ready = raw_input("Are you ready to do the test?(Please enter YES/NO) ")
while ready.lower() != "yes":
ready = raw_input("Are you ready to do the test?(Please enter YES/NO) ")
if ready.lower() == "yes":
print "Excelent! The test will start in any second."
if ready.lower() == "no":
print "Okay, tell me when you're ready. "
elif ready.lower() != "yes" and "no":
print "It okay if your're not sure. The test can start in any time you want. "
count = 0
if ready.lower() == "yes":
question1 = raw_input("\nQuestion 1\nWhat is your answer?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D: ")
if question1.lower() == "b":
print "Correct"
count = count + 1
elif question1.lower() != "b":
print "Wrong"
question2 = raw_input("\nQuestion 2\nWhat is your answer?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D: ")
if question2.lower() == "d":
print "Correct"
count = count + 1
elif question2.lower() != "d":
print "Wrong"
question3 = raw_input("\nQuestion 3\nWhat is your answer?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D: ")
if question3.lower() == "a":
print "Correct"
count = count + 1
elif question3.lower() != "a":
print "Wrong"
question4 = raw_input("\nQuestion 4\nWhat is your answer?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D: ")
if question4.lower() == "a":
print "Correct"
count = count + 1
elif question4.lower() != "a":
print "Wrong"
question5 = raw_input("\nQuestion 5\nWhat is your answer?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D: ")
if question5.lower() == "c":
print "Correct"
count = count + 1
elif question5.lower() != "c":
print "Wrong"
question6 = raw_input("\nQuestion 6\nWhat is your answer?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D: ")
if question6.lower() == "a":
print "Correct"
count = count + 1
elif question6.lower() != "a":
print "Wrong"
question7 = raw_input("\nQuestion 7\nWhat is your answer?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D: ")
if question7.lower() == "b":
print "Correct"
count = count + 1
elif question7.lower() != "b":
print "Wrong"
question8 = raw_input("\nQuestion 8\nWhat is your answer?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D: ")
if question8.lower() == "a":
print "Correct"
count = count + 1
elif question8.lower() != "a":
print "Wrong"
question9 = raw_input("\nQuestion 9\nWhat is your answer?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D: ")
if question9.lower() == "c":
print "Correct"
count = count + 1
elif question9.lower() != "c":
print "Wrong"
question10 = raw_input("\nQuestion 10\nWhat is your answer?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D: ")
if question10.lower() == "d":
print "Correct"
count = count + 1
elif question10.lower() != "d":
print "Wrong"
question11 = raw_input("\nQuestion 11\nWhat is your answer?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D: ")
if question11.lower() == "b":
print "Correct"
count = count + 1
elif question11.lower() != "b":
print "Wrong"
question12 = raw_input("\nQuestion 12\nWhat is your answer?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D: ")
if question12.lower() == "c":
print "Correct"
count = count + 1
elif question12.lower() != "c":
print "Wrong"
question13 = raw_input("\nQuestion 13\nWhat is your answer?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D: ")
if question13.lower() == "d":
print "Correct"
count = count + 1
elif question13.lower() != "d":
print "Wrong"
question14 = raw_input("\nQuestion 14\nWhat is your answer?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D: ")
if question14.lower() == "a":
print "Correct"
count = count + 1
elif question14.lower() != "a":
print "Wrong"
question15 = raw_input("\nQuestion 15\nWhat is your answer?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D: ")
if question15.lower() == "a":
print "Correct"
count = count + 1
elif question15.lower() != "a":
print "Wrong"
question16 = raw_input("\nQuestion 16\nWhat is your answer?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D: ")
if question16.lower() == "c":
print "Correct"
count = count + 1
elif question16.lower() != "c":
print "Wrong"
question17 = raw_input("\nQuestion 17\nWhat is your answer?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D: ")
if question17.lower() == "c":
print "Correct"
count = count + 1
elif question17.lower() != "c":
print "Wrong"
question18 = raw_input("\nQuestion 18\nWhat is your answer?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D: ")
if question18.lower() == "b":
print "Correct"
count = count + 1
elif question18.lower() != "b":
print "Wrong"
question19 = raw_input("\nQuestion 19\nWhat is your answer?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D: ")
if question19.lower() == "d":
print "Correct"
count = count + 1
elif question19.lower() != "d":
print "Wrong"
question20 = raw_input("\nQuestion 20\nWhat is your answer?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D: ")
if question20.lower() == "a":
print "Correct"
count = count + 1
elif question20.lower() != "a":
print "Wrong"
print "\n|Congratulation!,", "You have completed the test."
print "|Here's your result."
print "|Total score: " + str(count) + "/20"
division = float(count)/float(20)
multiply = float(division*100)
result = round(multiply)
print "|Total percentage is", int(result), "%"
if result >= 75:
print ("|Congratulations, you have passed!")
elif result <= 70:
print ("|Sorry, you have failed.\n|You should study more.")
更新了:谢谢你,我把循环部分弄下来了。现在我只需要知道如何允许用户输入他们自己的测试答案。为了澄清这一点,我的意思是允许用户在测试中输入自己的答案,然后开始测试。在
^{pr2}$
您应该使用类。这是一个基于你目前所拥有的快速模型,它更灵活,也更可重用。想想看,你有不同的问题,不同的成绩,这不是一个完整的问题,但会让你开始。在
我也提出了一些意见,希望代码是不言自明的:
用法:
^{pr2}$要开始测试,请调用
myquestions.start()
完成所有问题后,将调用
get_result
并打印您的统计信息。在我的代码并没有经过真正的优化,但这应该让您真正了解如何使用类来解决将来遇到的任何类似问题。在
你可以总是使用二维列表和for循环,使用Dict不会保持问题的顺序,或者你可能想使用有序Dict。。。在
在二维列表中,你可以写下问题和答案
创建循环就是为了避免这种编码。开玩笑。在
您可以创建一个答案列表,其中保留正确的选择。 然后,您可以使用
for
循环迭代这些问题。 下面是一个例子:相关问题 更多 >
编程相关推荐