如何简化我的 Python 测验?(让它更短)

我做了一个Python测验,大约有500行。我想知道如何使其更短并简化代码。这是我在测验中的一个问题的示例


while counter<3:

    def question(question,choices):                  

        print(question)                        

        for question in choices:             

            print(question) 


    print('\033[0m'"____________________________________________________________\n")


    question("Question 1. What is the real name of Batman?", ["A. Bruce Wayne", "B. Peter Parker", "C. Bruce Banner", "D. Bruce Waine"])

    answer = input().lower()


    if answer == "a":

        print('\033[32m'"\nNice job! ✔\n")

        score = score +1

        counter = 4

    elif answer == "bruce wayne":

        print('\033[32m'"\nGreat work! ✔\n")

        counter = 4

        break

    else:

        score = score - 1

        counter = counter +1

        if counter ==3:

            print('\33[31m'"\nIncorrect! ✘ The correct answer is A. Bruce Wayne\n")

        elif counter ==1 or 2:

            print('\33[31m'"\nIncorrect! ✘ Try again...\n")

    print('\033[0m''\033[04m'"Your score is ",score)


MMMHUHU
浏览 43回答 1
1回答

炎炎设计

下面是定义“问题”的自定义类的示例 - 然后您可以创建其中的许多问题,并以这种方式重用大量代码。class Question:&nbsp; &nbsp; def __init__(self, number, question, choices, correct, chances=3):&nbsp; &nbsp; &nbsp; &nbsp; self.number = number&nbsp; &nbsp; &nbsp; &nbsp; self.question = question&nbsp; &nbsp; &nbsp; &nbsp; self.choices = choices&nbsp; &nbsp; &nbsp; &nbsp; self.correct = correct&nbsp; &nbsp; &nbsp; &nbsp; self.chances = chances&nbsp; &nbsp; def print(self):&nbsp; &nbsp; &nbsp; &nbsp; print(self.question, '\n', '\n'.join(self.choices))&nbsp; &nbsp; def guess(self):&nbsp; &nbsp; &nbsp; &nbsp; while self.chances:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; answer = input().lower()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if answer in self.correct:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('\033[32m'"\nNice job! ✔\n")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return True&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.chances -= 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if self.chances == 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('\33[31m\nIncorrect! ✘ The correct answer is', self.correct)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return False&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('\33[31m'"\nIncorrect! ✘ Try again...\n")# Example setupscore = 0all_questions = [&nbsp; &nbsp; Question(&nbsp; &nbsp; &nbsp; &nbsp; 0,&nbsp; &nbsp; &nbsp; &nbsp; 'What is the real name of Batman?',&nbsp; &nbsp; &nbsp; &nbsp; ['A. Bruce Wayne', 'B. Peter Parker', 'C. Bruce Banner', 'D. Bruce Waine'],&nbsp; &nbsp; &nbsp; &nbsp; ['a', 'bruce wayne']&nbsp; &nbsp; ),&nbsp; &nbsp; Question(&nbsp; &nbsp; &nbsp; &nbsp; 1,&nbsp; &nbsp; &nbsp; &nbsp; 'Another question..',&nbsp; &nbsp; &nbsp; &nbsp; ['A. Answer 1', 'B. Answer 2', 'C. Answer 3', 'etc..'],&nbsp; &nbsp; &nbsp; &nbsp; ['b', '3'],&nbsp; &nbsp; )]for question in all_questions:&nbsp; &nbsp; question.print()&nbsp; &nbsp; correct = question.guess()&nbsp; &nbsp; if correct:&nbsp; &nbsp; &nbsp; &nbsp; score += 1我已经展示了一个示例,说明如何提出许多问题(在列表中),然后打印并一一猜测所有问题。让我知道你有什么问题(哈)。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python