The Alcorn Method · Python
If this, otherwise that: tiered prices
A complete step-by-step sheet, the way we write one for a learner. You will build two small programs that charge one price up to a cutoff and a cheaper price after it. Everything you need is on this page. Nothing here assumes you remember last week.
0 of 8 steps done. Your checkmarks are saved in this browser only.
· This is a demonstration sheet on an original problem. Learners' real coursework sheets stay private, between them and their mentor.
Start here, every time
- Open IDLE (or Thonny, or VS Code). You should see a window where you can type Python.
- File → New File. Save it right away as
tickets.pyin a folder calledpython-class. - To run: press F5 (IDLE) or the green Run button. The output appears in the other window.
Rules for this sheet
- Ask with
input( )and wrap it inint( ). No typing the number into the code. - One decision, made with
ifandelse. Not twoifs. - Spell a variable the same way every time it appears.
- Money prints with a dollar sign and exactly two decimals.
Words you need
- variable a labeled jar that holds one value. tickets is a jar with a number in it.
- input( ) the program asks a question and waits for you to type.
- int( ) turns what you typed into a whole number so math works on it.
- if / else a fork in the road. One side runs when the test is true, the other side when it is not.
- indent four spaces at the start of a line. It means 'this line lives inside the fork'.
- .format( ) dresses a number for printing: ${0:,.2f} means dollar sign, commas, two decimals.
Part 1 · Practice
Game tickets
The first 4 tickets are $25 each. Every ticket after the 4th is $20. Build it in four steps. Check the box when the screen matches.
-
Type these two lines. The first asks a question and turns the answer into a whole number. The second prints it back so you know the program heard you.
tickets = int(input("How many tickets? ")) print(tickets)You should see
How many tickets? 3 3
-
Delete the print line. Add the fork: if the number is 4 or fewer, every ticket is $25. Otherwise the first 4 cost $100 and the rest are $20 each. Note the colon at the end of the if line and the else line, and the four spaces at the start of the lines inside.
tickets = int(input("How many tickets? ")) if tickets <= 4: cost = 25 * tickets else: cost = 100 + 20 * (tickets - 4) print(cost)You should see
How many tickets? 6 140
-
Replace the last line. The {0:,.2f} part means: put the first value here, with commas, with exactly two decimals.
print("Total is ${0:,.2f}".format(cost))You should see
How many tickets? 6 Total is $140.00
-
Run it three times. Each answer below comes from the real program; if yours differs, compare your if line and your else line to Step 2, character by character.
You should see
How many tickets? 3 Total is $75.00 How many tickets? 4 Total is $100.00 How many tickets? 10 Total is $220.00
Part 2 · Your task
Study hall T-shirts
Save a new file as shirts.py. The first 10 shirts are $12 each. After 10, each extra shirt is $9. Same four steps, same shape. Where you see ____, fill in the blank yourself; the "You should see" box tells you whether you got it right.
-
Same shape as Part 1, Step 1. Fill the blank with the name you will use for the count. Use the same spelling every time it appears.
____ = int(input("How many shirts? ")) print(____)You should see
How many shirts? 5 5
-
Delete the print line. First 10 shirts are $12 each. After that, the first 10 cost $120 and each extra shirt is $9. Fill the four blanks: the cutoff, the single price, the flat amount, and the extra price.
shirts = int(input("How many shirts? ")) if shirts <= ____: cost = ____ * shirts else: cost = ____ + ____ * (shirts - 10) print(cost)You should see
How many shirts? 14 156
-
Replace the last line so the output says Order total, with the dollar sign, commas, and two decimals.
print("Order total is ${0:,.2f}".format(cost))You should see
How many shirts? 14 Order total is $156.00
-
Run it three times. The last one checks that the comma shows up.
You should see
How many shirts? 5 Order total is $60.00 How many shirts? 10 Order total is $120.00 How many shirts? 120 Order total is $1,110.00
If you see red text
Reading an error is a skill. Find the last line of the red text in the left column.
| What you see | What it means | What to do |
|---|---|---|
NameError: name 'ticket' is not defined | You spelled the variable two different ways. Python thinks they are two different jars. | Find every copy of the name and make the spelling match the first line exactly. |
SyntaxError: expected ':' | The if line or the else line is missing its colon at the end. | Add the colon. Both lines need one. |
IndentationError: expected an indented block | The line after if or else is not pushed in. | Put exactly four spaces at the start of each line inside the fork. |
TypeError: can't multiply sequence by non-int of type 'float' | You forgot int( ) around input( ). The answer came in as text, not a number. | Make the first line match Step 1: int(input(...)). |
ValueError: invalid literal for int() | You typed something that is not a whole number when the program asked, like 'six' or '6.5'. | Run it again and type digits only. |
It prints 140 instead of $140.00 | You are still on Step 2's print line. | Do Step 3: replace the last line with the .format version. |
Final check before you submit
Finished and graded? This is the kind of file we help you commit to your GitHub portfolio with a README in your own words. How tutoring works.