Sep-06-2022, 09:21 PM
(This post was last modified: Sep-06-2022, 09:21 PM by deanhystad.)
I don't agree with this approach.
If you just start typing code, hoping to come up with something that works, you end up doing design and implementation simultaneously. Both the design and the coding suffer because you never focus your attention on either. Design suffers because the design process is constantly interrupted by trying to remember commands, or reading documentation about a package you are using, or tracking down syntax errors. Coding suffers because you don't have an outline to direct your coding efforts. You are always coding to a moving target. Eventually you get the code to run(no more crashes), but it doesn't do what it is supposed to do. You often end up throwing away most of the code you wrote because it implements an algorithm that doesn't solve the problem.
If you had written down a design for your your quiz program it might have looked like this.
print question details
Whew! Got that working. Now where was I? Looking back at your written design document you notice you forgot to increment the score when the answer is correct.
just write the code how it first comes to mind, make it work, and you can optimize and make it better later on.Programming is mostly problem solving. The actual coding is a minor part. You should plan out how you want to solve the problem, then try to implement the solution in code. I strongly suggest writing down the design before you go anywhere near a computer. By doing design first, you split the programming task in half. When working on your design you can focus on the logic of your solution. You can even apply your solution, the algorithm, to to "test cases" to see if it works. All without writing a single line of code. Once the algorithm is solid, translate it into Python code. While coding you can focus on finding the right Python functions and language constructs (loops, functions, datatypes) to implement your algorithm.
If you just start typing code, hoping to come up with something that works, you end up doing design and implementation simultaneously. Both the design and the coding suffer because you never focus your attention on either. Design suffers because the design process is constantly interrupted by trying to remember commands, or reading documentation about a package you are using, or tracking down syntax errors. Coding suffers because you don't have an outline to direct your coding efforts. You are always coding to a moving target. Eventually you get the code to run(no more crashes), but it doesn't do what it is supposed to do. You often end up throwing away most of the code you wrote because it implements an algorithm that doesn't solve the problem.
If you had written down a design for your your quiz program it might have looked like this.
Quote:1 score = 0You might break out some of the steps to add detail or record unanswered questions.
2 start loop
3 print question
4 get input
5 validate input
6 if input is answer, add 1 to score
7 if input is not answer, print correct answer
8 end loop when there are no more questions
9 print final score
print question details
Quote:print out question to look like thisget input and validate details
What is blah blah blah?
1. choice 1
2. choice 2
3. choice 3
Quote:start loopChecking if answer is correct and update score
get user input
loop until input is a number 1, 2 or 3.
What should I do if the input is not valid? Print a message? Should I print the question and choices again?
How do I make sure the input is a number?
Quote:If user input = answerAfter you got your program running you would run some tests, maybe with just 1 question. After all, why write code for a dozen questions until you know things work with 1.
print congratulations
add 1 to score
otherwise
print Sorry, the correct answer is correct answer
How do I get the answer back from the user input validation? Do I use a variable, or should I return it from the function?
How do I determine if the user selected the right answer? What should input validation return? A number (1, 2, 3)? Should the
correct answer also be a number? Or do I want the input validation to return the answer as text, like "Mercury", and the correct answer would also be text. That would make it easier to print out the sorry message.
answer = 0 print("What is your favorite color?\n1. Red\n2. Green\n3. Blue") proper_answer() if answer != 2: print("The correct answer is Green")When you ran the test program you'd notice the no matter what you type for the answer it is always wrong. You would go back to your design and notice your had written yourself a note:
Quote:How do I get the answer back from the user input validation? Do I use a variable, or should I return it from the function?You might step through the program with the debugger, or add some print statements and notice that even though "answer" changes inside proper_answer(), that answer is always zero outside the function. You would investigate why that is happening and learn about variable scope, that variables assigned inside a function only exist inside the function, even if they have the same name as a variable assigned outside the function. You might learn about the "global" scope specifier that allows assigning global variables from inside a function. You would read that global variables should be uses sparingly because as programs grow in size it becomes increasingly difficult to keep track of all the places where a global variable is modified. So you would modify your proper_answer() function to return the user input as @kaega2 recommends.
Whew! Got that working. Now where was I? Looking back at your written design document you notice you forgot to increment the score when the answer is correct.