Python Week – Day 3 – Quiz App

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 the code by creating a quiz_common module

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?

A coin flip is a simple game of chance that can have one of two outcomes: heads or tails. When you flip a coin, the outcome is determined by a combination of the initial conditions of the coin (i.e., its weight distribution, air resistance, initial velocity, etc.) and external factors such as air currents, surface friction, and how the coin lands.

In a fair coin flip, the probability of getting heads or tails is exactly 50%, assuming the coin is not biased in any way. Therefore, if you flip a fair coin multiple times, you would expect to get heads about half the time and tails about half the time.

In a programmatic implementation of a coin flip game, we can simulate the coin flip by generating a random number between 0 and 1. If the result is less than 0.5, we can consider it heads, and if it’s greater than or equal to 0.5, we can consider it tails. This method simulates the 50/50 probability of a fair coin flip and allows us to program a simple and fair coin flip game in Python.

The logic of the game is straightforward – the player chooses whether to flip a coin, and the program uses the logic of the coin flip to randomly generate either “heads” or “tails.” The player’s goal is to predict which outcome will occur, and they win if their prediction is correct.

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 app, I will work you through the following steps:

  1. Create a new project workspace
  2. Just print “flip a coin” and “It’s a head” to console
  3. 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, print HEAD, else print TAIL
  4. Ask the user for input for random_val (instead of pretending it is always 0.50)
  5. Instead of getting the random value from the user, get it by calling seed() and random() from the Python random module.
  6. Improve the “randomness” by using the current timestamp as a “seed”. The coin flip should now be fair.
  7. Add the for loop, so that the coin is flipped 10 times in a roll
  8. Keep a tally of how many heads and tails are there after 10 coin flips

Java Streams Code Snippets

What is a Java Stream?

Java Stream is a sequence of elements supporting the sequential and parallel aggregate operation. This example shows an aggregate operation using Stream and IntStream

int sum = widgets.stream()
                 .filter(w -> w.getColor() == RED)
                 .mapToInt(w -> w.getWeight())
                 .sum();

In this example (from Oracle Stream doc), widgets is a Collection<Widget>. A stream of Widget objects was created via Collection.stream(), filter it to product a stream containing only the red widgets, and then transform it into a stream of int values representing the weight of each red widget. Finally this stream is summed to produce a total weight.

 

This post lists Java Streams code snippets that I find very useful