我已经开发了一个测试,我希望这样做,以便用户可以在测试实际开始之前输入他们自己的测试答案。这是我到目前为止所拥有的。如果我使用字典而不是多次执行原始输入会更好吗?或者有一种方法我可以问问用户他们的答案而不这样做。谢谢!
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.")
已更新:谢谢,我让循环部分失效了。现在我只需要知道如何允许用户输入他们自己的测试答案。为了澄清,我的意思是允许用户输入他们自己的答案密钥进行测试,然后开始测试。
answers = ["b", "d", "a", "a", "c", "a", "b", "a", "c", "d", "b", "c", "d", "a", "a", "c", "c", "b", "d", "a"]
count = 0
for i in range(20):
answer = raw_input("\nQuestion " + str(i+1) + "\nWhat is your answer?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D: ");
if answer.lower() == answers[i]:
print "Correct"
count = count + 1
else:
print "Wrong"
答案 0 :(得分:0)
创建循环是为了避免编码。 --kidding。
您可以创建一个答案列表,其中保留了正确的选项。
然后,您可以使用for
循环迭代问题。
这是一个例子:
answers = ["b", "d", "a", "a", "c", "a"]
count = 0
for i in range(len(answers)):
answer = raw_input("\nQuestion " + str(i+1) + "\nWhat is your answer?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D: ");
if answer.lower() == answers[i]:
print "Correct"
count = count + 1
else:
print "Wrong"
答案 1 :(得分:0)
你总是可以使用2d列表和for循环,使用Dict不会保持问题的顺序,否则你可能想要使用有序的Dict ......
在2d列表中,您可以写下问题及其答案
questions = [["\nQuestion {0}\nWhat is your answer?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D: ","b"],
["\nQuestion {0}\nWhat is your answer?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D: ","d"],
["\nQuestion {0}\nWhat is your answer?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D: ","a"],
["\nQuestion {0}\nWhat is your answer?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D: ","c"],
["\nQuestion {0}\nWhat is your answer?\n \nA\nB\nC\nD\n\nPlease enter A, B, C or D: ","d"]]
count=0
for n,i in enumerate(questions):
quest = raw_input(i[0].format(n))
if quest.lower() == i[1]:
print "Correct"
count = count + 1
elif quest.lower() != i[1]:
print "Wrong"
答案 2 :(得分:0)
您应该使用类。这是一个基于你到目前为止的快速模拟,它更灵活,也可以重复使用。想想你有不同等级的不同问题,但它不是一个完整的问题,但会让你开始。
我也提出了一些意见,希望代码不言自明:
class Questions:
def __init__(self, title='', answers=[]):
# title can then be changed to whatever you like (A,B,C,D,E,F... etc.)
self.title = title
# answers will be a python list, just like you have now
self.answers = answers
# we use a dictionary to store question N and its correct answer
self.questions = {}
# no of questions answered correctly
self.count = 0
# a boolean flag whether the test has been completed
self.completed = False
# check if answers are set, then loop through the list and set the key/value pair
if self.answers:
for i, answer in enumerate(self.answers):
self.questions['Question ' + str(i+1)] = answer
def start(self):
if not self.answers:
print "No question and answer set yet"
return
# you only need a YES answer, so set a while loop to break only on YES
while raw_input('Are you ready to do the test? (Please enter YES/NO) ').lower() != 'yes':
pass
# reset the count in case people break in the middle of the questions
self.count = 0
# just iterate the list, and get the key/value pair as a question
for i, answer in enumerate(self.answers):
question = 'Question ' + str(i+1)
ans = raw_input(question +'\n' + self.title)
if ans.lower() == self.questions[question]:
print "Correct"
self.count += 1
# quite obvious, if not Correct, that's Wrong
else:
print "Wrong"
# once all questions answered, set completed flag as True
self.completed = True
# now go to get_result to print all stats you want
return self.get_result()
def get_result(self):
# to check if all questions are actually completed
if not self.completed:
print "You still haven't completed the test yet"
return
print "\n|Congratulation!,", "You have completed the test."
print "|Here's your result."
print "|Total score: " + str(self.count) + "/20"
division = float(self.count)/float(20)
multiply = float(division*100)
result = round(multiply)
print "|Total percentage is", int(result), "%"
if result <= 70:
print ("|Sorry, you have failed.\n|You should study more.")
# well again, if result is not <=70, it will be above.
else:
print ("|Congratulations, you have passed!")
用法:
# set the title so do not repeat yourself on each question asked
# and the answers will be a list of the Correct answer, you get the idea
myquestions = Questions(title='What is your answer?\nPlease enter A, B, C or D: ', answers=['b','d','a','c','a','b','a','c','d','b','c','d','a','c','c','b','d','a'])
# After you instantiated your class, the __init__ is called, and so the questions dictionary is created automatically
myquestions.questions
{'Question 1': 'b',
'Question 10': 'b',
'Question 11': 'c',
'Question 12': 'd',
'Question 13': 'a',
'Question 14': 'c',
'Question 15': 'c',
'Question 16': 'b',
'Question 17': 'd',
'Question 18': 'a',
'Question 2': 'd',
'Question 3': 'a',
'Question 4': 'c',
'Question 5': 'a',
'Question 6': 'b',
'Question 7': 'a',
'Question 8': 'c',
'Question 9': 'd'}
要开始测试,请致电myquestions.start()
myquestions.start()
Are you ready to do the test? (Please enter YES/NO) yes
Question 1
What is your answer?
A
B
C
D
Please enter A, B, C or D: b
Correct
...
Question 18
What is your answer?
A
B
C
D
Please enter A, B, C or D: s
Wrong
|Congratulation!, You have completed the test.
|Here's your result.
|Total score: 1/20
|Total percentage is 5 %
|Sorry, you have failed.
|You should study more.
完成所有问题后,系统会调用get_result
并打印您的统计信息。
我的代码并未真正优化,但这应该让您了解如何使用 Class 来解决您将来遇到的任何类似问题。