Python Week – Day 3 – Quiz App

Blog Status: Draft (In the progress of adding more explanation at each step) but is 90% complete

On Day 2, we created a Coin Flip App to demonstrate Import, If-Else, For Loop, Red From User Input, Random Number, and Logging.

Today, let’s create a Python Quiz App that should be useful for studying for quizzes. This project demonstrates a few more core python concepts (Dictionary, Function, Read from a File).

Below are the steps to create the Python Quiz App:

  • Step 1: Create a words dictionary, and randomly select an item to ask a question
  • Step 2: Populate the words dictionary from a CSV file
  • Step 3: Ask the user to choose which quiz to take, then randomly select a question from that quiz
  • Step 4: Ask the user to select a quiz category, then ask randomly selected questions from a randomly selected quiz
  • Step 5: Further clean up code by creating a quiz_common module

Read more

Python Week – Day 2 – Simple Coin Flip Game

Follow the instruction from Day 1 to create another python project. In this python project, create a file called flip.py

How does a Coin Clip game work?

Normally, it works by someone tossing a coin, which can be a tail or a head, and another person guess before toss. The guesser wins if the guess matches.

If this coin is a perfectly made coin, there should be a 50/50 chance that it would be head/tail.

A very simple coin flip game could work as follow:

  1. Ask for head or tail
  2. Flip a coin
  3. If the guess matches the result, then it’s a win

To build this game, I will work you through the steps, the simplest first, then onto

  1. Just print “flip a coin” and “It’s a head” to console
  2. Create a constant variable to hold the value of the coin flip, but pretend that the value is always “head”
  3. For better style and to reduce duplicate code, create a new file to store the constant variables. Use the constant variables in this new module.
  4. Create a placeholder variable called “random_val”, but set it to “0.50” (pretend it’s always 0.50) . Add code to check if random_val is larger than 0.50, if so, pring HEAD, else print TAIL
  5. Ask user for input for random_val (instead of pretend it is always 0.50)
  6. Instead of getting the random value from the user, get it by calling seed() and random() from the Python random module.
  7. Improve the “randomness” by using the current timestamp as “seed”. The coin flip should now be fair.
  8. Add the for loop, so that the coin is flipped 10 times in a roll
  9. Keep a tally of how many heads and tails are there after 10 coin flips
  10. Ask the user to enter the number of coin flips (instead of hard-coded it to 10)
  11. Beautify the output

Read more