简体   繁体   English

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

[英]Python: How to let the user input their own solution to the answer key

I have developed a test and I want to make it so the user can input their own answers to the test before the test actually starts. 我已经开发了一个测试,并且想要进行测试,以便用户可以在测试真正开始之前输入自己的答案。 Here is what I have so far. 这是我到目前为止所拥有的。 Would it be better if I used a dictionary instead of doing raw input multiple times or is there a way I could just ask the user for their answers without doing that. 如果我使用字典而不是多次进行原始输入会更好,还是有一种方法我可以不问而就直接向用户询问他们的答案呢? Thanks! 谢谢!

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

updated : thank you I got the loop part down. 更新 :谢谢,我把循环部分讲了下来。 Now I just need to know how I can allow the user to input their own answers to the test as well. 现在,我只需要知道如何允许用户输入自己的测试答案即可。 To clarify, by this I mean allow the user to input their own answer key to the test and then begin the test. 为了澄清,我的意思是允许用户输入自己的答案键进行测试,然后开始测试。

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"

The loops are just created to avoid from this coding. 只是为了避免这种编码而创建循环。 --kidding. --kidding。

You can create an answers list where the correct choices are kept. 您可以创建一个答案列表,保留正确的选择。 Then you can iterate over the questions with a for loop. 然后,您可以使用for循环遍历问题。 Here is an example: 这是一个例子:

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"

you can always use 2d lists along with for loop, using Dict will not maintain the order of question, other wise you might want to use ordered Dict... 您可以始终将2d列表与for循环一起使用,使用Dict不会保持问题的顺序,否则,您可能要使用有序Dict ...

in 2d list you can write down question as well as its answers 在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"

You should use a Class . 您应该使用Class Here is a quick mock up based on what you've got so far, it's more flexible, and reusable too. 这是根据您到目前为止所获得的内容进行的快速模拟,它更加灵活并且可重复使用。 Think of you have different sets of questions for different grades, it's not a complete one but will get you started. 考虑一下您对不同年级有不同的问题集,虽然这不是一个完整的问题,但可以帮助您入门。

I have also put some comments and hopefully the code is self-explanatory: 我也发表了一些评论,希望代码是不言自明的:

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

Usage: 用法:

# 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'}

To start your test, call myquestions.start() 要开始测试,请调用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.

At the completion of all questions, the get_result will be called and print your stats. 完成所有问题后,将调用get_result并打印您的统计信息。

My code is not really optimized, but this should really get you to understand how to use Class to solve any similar problems you have in future. 我的代码并未真正优化,但这确实可以使您了解如何使用Class解决将来遇到的任何类似问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让用户在python 3.6.4中输入小数? - How do you let a user input a decimal in python 3.6.4? 如何让用户在 sqlite3、python3 中输入 TABLE 名称? - How to let user input TABLE name in sqlite3, python3? 我如何让我的python hangman游戏的用户输入他们自己的单词? - How can I let the users of my python hangman game input their own words? 如何让用户输入所需的输入行数,直到他们在Python中输入“停止”字? - How to let the user input as many lines of input as they want until they input the “stop” word in Python? 如何从用户输入在python中添加一个键 - how to add a key in python from user input 如果用户输入无效答案,如何重做输入 - How to redo an input if user enters invalid answer 如何接受用户输入以让用户指定他们想要访问 python 程序的哪个方面 - how to take user input to let the user specify which aspect of a python program they want to access 如何在 python 中创建一个正确答案的定时输入? - How to create a timed input in python with a correct answer? Python Fabric:如何回答键盘输入? - Python Fabric: How to answer to keyboard input? 如何让用户将“无”添加到在 Python 字典中设置为特定键值的特定数组? - How to let the user add 'None' to an specific array that's is set as value of an specific key in a dictionary on Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM