Skip to content

✍️ Writing

✏️ Section undergoing re-write. Come back soon.

To learn a language, you usually practice speaking and writing. Start with basic sentences, then work up to paragraphs, then essays, then stories and reports.

Python is a written language only. We wrote some simple sentences in the previous section. Now let's try writing short paragraphs.

Reusing code

Other smart, more experienced programmers have written Python code that we can reuse.

Some of this reusable code comes with Python. It's called the Standard Library. The standard library isn't loaded into the intepreter by default, so we have to import it.

For example,

import random

# Gets a random integer from 1 to 10
my_secret_number = random.randint(1, 10)
print(my_secret_number)
import random
import string

# Gets3 random latin letters
rand_letters = random.choices(string.ascii_letters, k=3)
print(rand_letters)

Writing Paragraphs

1. Login Authentication

Write a passive-agressive login authenticator.

  1. Let the user access the system if they give the correct password.
  2. But if they try to guess "password" tell them they need to try harder than that to get in!
  3. And if they try "abc123", give an exasperated response.
  4. And if they try anything longer than 12 characters, gently remind them that the password can't be that long.
  5. Also, the user only has 5 attempts to log in. If they fail all 5, print a message saying to try again later.
  6. Otherwise, tell them they entered the wrong password.
password.py
1

Possible solution
password.py
1

2. Fetching Useless Facts

When you type in a website URL in your browser, your browser has to download the website from another computer. Your browser asks the other computer for the website, and the other computer always responds with a status code.

Two important status codes are:

  • 200: everything is ok and the website has been successfully transferred to your browser
  • 404: the other computer couldn't find the website files, so it says "page not found"

Go to https://uselessfacts.jsph.pl/api/v2/facts/random

If you see some text, great, the other computer returned a status code of 200.

Now try https://uselessfacts.jsph.pl/api/v2/facts/random1

You probably got a 404 "page not found" status code.

Based on what you just learned about status codes, try to complete the Python script below:

ueslessfact.py
1

Possible solution
ueslessfact.py
1

3. Changemaker

Prompt the user to enter an amount of dollars and cents. For example $1.18. Display the number of quarters, dimes, nickels, and pennies to make that amount, making sure to maximize the amount of higher-value coins.

Examples:

  • If the user entered $1.18 it should output: 4 quarters, 1 dimes, 1 nickels, 3 pennies
  • If the user entered $1.02 it should output: 4 quarters, 0 dimes, 0 nickels, 2 pennies
Possible solution
changemaker.py
1

4. Powerball

Write a program that simulates the chances of winning the Powerball , and how much money you would realistically need to spend on to win.

"To play Powerball, you must fill in a draw ticket by choosing five main numbers up to 69 and a sixth 'Powerball' number from 1-26"

Use the code below to get started:

1) Calculate your earnings

Matching Draw Amount ($)
5 + Powerball Jackpot ($60 million)
5 US $1,000,000
4 + Powerball US $50,000
4 US $100
3 + Powerball US $100
3 US $7
2 + Powerball US $7
1 + Powerball US $4
0 + Powerball US $4

2) For notable winnings (Grand prize, 1 million, and 50 thousand), print a message

3) Compare the stats from the simulation to the theoretical statistics. Are they close?

powerball.py
1

Possible solution
powerball.py
1