Python:如何让用户输入自己的答案键

2024-05-29 02:57:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我开发了一个测试,我想让用户在测试开始之前输入自己的答案。这是我到目前为止的情况。如果我使用字典而不是多次进行原始输入会更好,还是有一种方法可以直接向用户询问答案而不必这样做。谢谢!在

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}$

Tags: answerinputyourrawifiscountlower
3条回答

您应该使用。这是一个基于你目前所拥有的快速模型,它更灵活,也更可重用。想想看,你有不同的问题,不同的成绩,这不是一个完整的问题,但会让你开始。在

我也提出了一些意见,希望代码是不言自明的:

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!")

用法:

^{pr2}$

要开始测试,请调用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并打印您的统计信息。在

我的代码并没有经过真正的优化,但这应该让您真正了解如何使用来解决将来遇到的任何类似问题。在

你可以总是使用二维列表和for循环,使用Dict不会保持问题的顺序,或者你可能想使用有序Dict。。。在

在二维列表中,你可以写下问题和答案

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"

创建循环就是为了避免这种编码。开玩笑。在

您可以创建一个答案列表,其中保留正确的选择。 然后,您可以使用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"

相关问题 更多 >

    热门问题