Deliver: quiz.py

Throughout the semester, I have been introduced to a whole new world of syntaxes, from learning that python doesn’t need a semicolon (;) to end it’s statements like other programming languages I have used, to learning to create programmer-defined functions and calling them. I have gotten more comfortable with the larger concepts behind certain functions and learned how they function and why they act the way they do rather than just knowing how to use it.

Develop: quiz.py

There needs to be a global variable in order for this program to work since the variable that keeps track of the number of questions the user gets right, SCORE, is used in more than one function. Unless SCORE is declared as a global variable, it defaults to being a local variable, where it can only exist either within the main code of the program or only within the programmer-defined function. By declaring SCORE as a global variable, we can now use SCORE both within the main code of the program and within the programmer-defined function.

I will use multiple .py files to keep my code organized. I will split my program up into 3 .py files named quizFC.py, quizFC_data.py, and quizFC_functions.py. In quizFC_data.py, I will store all the variables that the program will need to run. In quizFC_functions.py, I will have all the programmer-defined functions stored. Finally, quizFC.py will be where I call the programmer-defined functions and introduce the program to the user.

 

Define: quiz.py

The different sets of variables I will need to make this program work will be listed below:

  • q0-q9: Contains the question that is outputted to the user
  • q0CorAns-q9CorAns: Contains the correct answer to the associated question
  • SCORE: A global variable that is used to keep track of the number of correct answers the user selected
  • grade: The user’s final grade in a percentage, will be outputted to the user
  • ansU: Variable that is reused to hold the user’s input
  • check: A boolean variable that is used as an escape condition for the question while loop
  • showAnsCheck: A boolean variable that is used as an escape condition for the show correct answer while loop

Discover: quiz.py

A while loop is a type of control flow statement that allows a block of code to be executed repeatedly based on a boolean condition. This means that a while loop will have a block of code run until a certain condition is met.

A triple-quoted string is a type of string that allows the programmer to create strings that span more than one line without using multiple print() statements.

Custom functions allow programmers to define their own functions. This means that a programmer can create a block of code that can be run multiple times by calling it rather than having to type out that entire block of code each time it is needed.

In quiz.py, I will need to use all three programming components as it will streamline the code. Using a while loop will allow my program to provide specific feedback to the user if their inputs are invalid, and prevent them from moving onto the next question until they enter an acceptable answer. Using a triple-quoted string will allow for easy formatting of the quiz. The use of programmer-defined functions will cut down on the length of the program, and make the code more readable as there will only be one while loop that will output the question, receive an input, check to see if it’s correct or acceptable and increment the score if it’s correct, and provide feedback if the input is not acceptable.

Algorithm Comparisons

The required algorithm will need to ask the user a question and provide 4 answer choices that the user will select the answer from. The algorithm will need to be able to filter through the possible inputs from the user and either deem the input invalid, where the input is not even acceptable (if asking for a number between 1 and 4 and the user inputs either a letter or a number greater than or less than the range), deem the input acceptable, where the input is what the program is expecting, and the correct input, where the input is the input associated with the correct answer. At each stage of the sorting, the algorithm will need to provide useful and appropriate feedback.

Screen-Shot-2018-12-18-at-11.17.55-AM.png

One problem with this sample is that there are some lines of code that are unnecessary, bogging down the program. Lines 27 and 30 are unnecessary as they are located within an if, elif loop structure, where it will naturally continue.  In addition to that, the program does not take into account what is wrong with the user’s input when providing feedback. In order to make the program more efficient, you can move lines 31 to 33 to the top of the if, elif structure so that the program doesn’t need to run through the other 2 loops before identifying that the user has selected the correct answer.

Screen-Shot-2018-12-18-at-11.21.33-AM.png

One advantage of this sample is how little lines of code it uses to accomplish the same task, which generally means that it’s more efficient. This is because the programmer initially sorts the input into two different categories: integer, and not an integer. If the input is not an integer, then the program will skip directly to line 29, which will prompt the user to enter an input. One of the downsides of this sample is how the feedback is not tailored for the specific error with their input. If the input is an integer, then it will check if it’s the correct answer first, doing so makes it so that if the user selects the correct answer, the program will be able to know that they selected the correct answer and not have to check to see if it’s an acceptable answer, since a correct answer has to be acceptable.  If the user input is an integer but is not correct, then it will check to see if it’s acceptable. If it is acceptable, then the program will change Q1 to true, and if it is not an acceptable answer, it will prompt the user for an answer.

Screen Shot 2019-01-14 at 10.42.06 AM.png

I think this will be the best algorithm to use for question.py, as it is the most efficient algorithm that will take into account the case in which the user enters their input. The algorithm will first determine if the user input is a string, and if it isn’t then it will jump directly to the except ValueError, skipping the entire algorithm. After that, the algorithm will parse the input into the lowercase version of their string, and then check to see if the input is the correct answer. If it is, then it will provide feedback, and exit the loop since a correct answer has to be acceptable. If the user input is not correct, then it will check to see if it is acceptable, and if it is then it will provide feedback and exit the loop. However, if the input is not acceptable then it will provide feedback to the user and prompt them for an answer again.