Instructions
1. Instructions run from top to bottom
A Python program is a list of instructions. Python runs them one at a time, from the top line to the bottom line. Each instruction goes on its own line.
print("First") # runs 1st print("Second") # runs 2nd print("Third") # runs 3rd
Because Python reads from the top down, a variable must be created before you use it.
name = "Ava" print(name) # prints Ava
print(name) # Error! name does not exist yet name = "Ava"
Two more rules for writing instructions:
- Python is case sensitive.
printworks, butPrintdoes not. - Every opening parenthesis
(needs a closing parenthesis).
2. Comments
A comment starts with #. Python ignores everything from the # to the end of that line. Programmers use comments to explain code to people.
- Full-line comment: the whole line starts with
#, so the whole line is skipped. - Inline comment: code comes first, then
#. The code runs, and only the part after#is skipped. - Commenting out code: putting
#in front of a line of code turns it off without deleting it. - Careful: a
#inside quotes, like"#1 fan", is part of the text, not a comment.
3. Indentation
Indentation means spaces at the start of a line. Python pays close attention to these spaces. Later in the course you will learn when to indent. For now, the rule is simple: every line starts at the left edge, with no spaces in front.
On this page, each orange dot · stands for one space.
name = "Ava" print(name)
name = "Ava" ··print(name) # 2 extra spaces
Spaces in the middle of a line, like x = 5, do not cause an error. Only spaces at the start do. (Style tip: one space on each side of =, like x = 5, is easiest to read.)
Error messages you may see in the terminal
IndentationError: unexpected indentmeans a line has extra spaces at the start.NameError: name 'name' is not definedmeans a variable was used before it was created, or a word was spelled wrong (likePrint).SyntaxError: '(' was never closedmeans a closing parenthesis)is missing.
How this works
- Enter your first name, last name initial, and class period, then click Start Assignment. Your start time is recorded.
- The assignment opens in a Python editor with four files (tabs). Complete every orange dashed box and drop down.
- Click ▶ Run on any part to see what Python would print in the terminal. Run does not grade you, so check your work carefully.
- When you finish all four tabs, click Submit for Score. Your end time is recorded and your score appears. Answers cannot be changed after submitting.
| Part | Task | Points |
|---|---|---|
| 1 | Instructions: put lines in the right order and fix two broken lines | 8 |
| 2 | Comments: decide what Python runs or ignores, then write comments | 8 |
| 3 | Indentation: spot the lines that cause an error | 5 |
| 4 | Indentation: fix a program by removing extra spaces | 4 |
| Total | 25 |
Student Information
- Lines 3 to 6: use the drop downs to put the four instructions in order. Use each instruction once.
- Line 3 creates the variable
name. A variable must be created before any line can use it. - Lines 4 to 6 print the first three lines of the output below, in order.
- Line 3 creates the variable
- Lines 9 and 10: each line has one mistake. Fix only the mistake. These two lines print
See you tomorrowandGoodbye. - Python checks the whole file for mistakes before it runs anything. Until lines 9 and 10 are fixed, Run shows an error instead of the output.
- Lines 4 to 9: use the drop down to decide if Python runs the line or ignores it. A line with an inline comment still counts as running.
- Line 12: write your own comment that explains line 13.
- Line 16: change the line so Python skips it (comment it out).
Tip: when you click Run, Python stops at the first error it finds, so a line below that error could have a problem too.
Name: Ana Diaz, but some lines have extra spaces at the start. Remove the extra spaces so every line starts at the left edge. Do not change anything else. If a line is already correct, leave it alone.Tip: click at the very start of a box and press Delete to remove a space.