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:
- Ask for head or tail
- Flip a coin
- 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
- Just print “flip a coin” and “It’s a head” to console
- Create a constant variable to hold the value of the coin flip, but pretend that the value is always “head”
- 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.
- 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
- Ask user for input for random_val (instead of pretend it is always 0.50)
- Instead of getting the random value from the user, get it by calling seed() and random() from the Python random module.
- Improve the “randomness” by using the current timestamp as “seed”. The coin flip should now be fair.
- Add the for loop, so that the coin is flipped 10 times in a roll
- Keep a tally of how many heads and tails are there after 10 coin flips
- Ask the user to enter the number of coin flips (instead of hard-coded it to 10)
- Beautify the output