3/14/2023 0 Comments Simple math quiz python![]() # Question (instance) #answer keys #answer Self.points = points #points for correct answer #create a list of questions, answers and answer keys #("\n" will put the text on a new line in the console) rrect_answer = correct_answer #correct answer Self.answer_keys = answer_keys #options for the answer Self.question = question #question to be asked ''' class Quiz: def _init_ (self, question, answer_keys, correct_answer, points): In this class a quiz has 3 attributes - the question the valid choices and the answer #Quiz Class '''Ĭlass: A class is a blueprint for a particular object On the developer side using, classes makes upkeep and future development a lot easier further increasing the chance of adoption. So things like clearly identifying how to exit the program and informing the user to choose the answer key not type the actual answer itself, being able to deal with whitespace, upper/lower case and giving the user a chance to repeat the question if they have entered an invalid option were key to making the program as easy as possible to use. The greater the complexity on the front end, the less chance of adoption. There is a nested while loop in the for loop, this handles cases where the user enters in an option that's not on the list, for example "J" the user will receive a message identifying their answer is not valid and will be re-prompted for a valid optionĮven though this program is relatively simple, a bit of thought still needed to go into constructing the script to make it as easy as possible to use. There is an if statement in the for loop to handle users wanting to leave the quiz early, if they type exit the program will end at any stage. This is to make life easier for the user so they don't have to worry about answers being case sensitive or accidentally hitting the tab and space bar. the lower function converts the user entry to lowercase and the strip function remove whitespace. The for loop presents each question in sequential order, within the for loop the user is prompted for an answer. I set the score initially as zero as the user hasn't answered any questions, every time a question is answered correctly, the points for that question are added to the score variable. Using a class to create this quiz offers a strong benefit, I can add a new question, or remove a question all in one place, I don't have to alter multiple lines of code which can increase the risk of bugs.įinally i create a normal function called run test which basically runs the quiz. ![]() With scalability in mind I used a list for the answer keys, this allows me to quickly add a fourth, fifth or whatever option for that question, it also allows different questions to have different choice amounts. the "\n" will just put the text on a new line in the console. Again think of it like when I can quiz it automatically generates a unique index i.e. In the next step i created a list of questions, using the quiz class to do so, if you note in the init function I have 5 arguments but only have 4 arguments entered when i call the quiz class, this is because self is automatically assigned every time I call the quiz class so I don't need to declare it. once self is declared I can pass my other variables which will be the question, answer keys (these will be the choices the user can select), correct answer to each question, and the points allocated to each question. Self is a special naming convention to declare an instance, you can call self anything but it's best to stick with python convention, the first variable passed to the init method will always be the instance, think of it like question 1, question 2 etc. The init method is a python standard for classes, it's a constructor meaning it's an initialisation method that defines key features of the instance (questions). In this class i only have one method, a method is a function that's associated with a class. I like to think of the class being a table with columns and the instance being a record in that table. I created a class called quiz, a class is a blueprint of an object or instance, in this case the instance is a question.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |