Python Forum
Noob here. Random quiz program. Using a while loop function and alot of conditionals.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Noob here. Random quiz program. Using a while loop function and alot of conditionals.
#6
I don't agree with this approach.
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 = 0
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
You might break out some of the steps to add detail or record unanswered questions.
print question details
Quote:print out question to look like this

What is blah blah blah?
1. choice 1
2. choice 2
3. choice 3
get input and validate details
Quote:start loop
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?
Checking if answer is correct and update score
Quote:If user input = answer
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.
After 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.
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.
Reply


Messages In This Thread
RE: Noob here. Random quiz program. Using a while loop function and alot of conditionals. - by deanhystad - Sep-06-2022, 09:21 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  problem program runs in a loop jasserin 0 931 May-18-2024, 03:07 PM
Last Post: jasserin
  QUIZ GUI Form_When admin panel is open, main quiz form is getting freeze Uday 4 1,991 Aug-25-2023, 08:24 PM
Last Post: deanhystad
Question Doubt about conditionals in Python. Carmazum 6 3,394 Apr-01-2023, 12:01 AM
Last Post: Carmazum
  [ESP32 Micropython]Total noob here, I don't understand why this while loop won't run. wh33t 9 4,081 Feb-28-2023, 07:00 PM
Last Post: buran
  Function not scriptable: Noob question kaega2 3 2,322 Aug-21-2022, 04:37 PM
Last Post: kaega2
  conditionals based on data frame mbrown009 1 1,654 Aug-12-2022, 08:18 AM
Last Post: Larz60+
  Efficiency with regard to nested conditionals or and statements Mark17 13 5,790 May-06-2022, 05:16 PM
Last Post: Mark17
  Nested conditionals vs conditionals connected by operators dboxall123 8 4,915 Feb-18-2022, 09:34 PM
Last Post: dboxall123
  Program for random sum Cristopher 7 3,348 Jan-15-2022, 07:38 PM
Last Post: Cristopher
  I am writing a car rental program on python. In this function I am trying to modify aboo 2 3,971 Aug-05-2021, 06:00 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020