input(), print() and a text menu: the same logic as the turtle courses, as text in and text out. Every Python extension starts here.
Each lesson works two ways. Students can work through the online tutorial at their own pace (it checks their answers and saves their progress in their browser), or you can teach it to the whole class from the slides, then send students into the tutorial for the Modify and Make steps.
Every lesson guide has plain-language notes, the answers to every check, a solution for every task, and slides and a worksheet for Waypoints Prepare. These guide pages are not linked from student pages, but they are not secret either: keep printed Teacher editions for answers you do not want students to find.
Whole-course cheat sheet: Open to print (every command, grouped by unit: a handout for the final challenge)
Show text, ask a question, store the answer and join numbers onto text.
A console program works with text instead of pictures. It shows a line, waits for someone to type an answer, and replies. Three pieces do almost all of it:
print("...") shows a line of text. Text goes in quotes, and the quotes are not shown.+ joins pieces of text into one: "Hello, " + name. It adds nothing, so a space only appears where it is inside the quotes.input("question") shows the question, waits, and gives back what was typed. Store it with name = input(...).A number cannot be joined onto text: "Score: " + 5 is an error. str(5) makes a text copy that can be joined.
A console program is a person at a counter: they say something, listen, and reply.
Where it breaks: the person at the counter hears a whole sentence. The program only holds what it was told to store, so an answer that is not stored in a name is gone.
name = input("Name? ") then print("Hi " + name). Click the line in the console, type, and press Enter."Hi " and run it again. Where did the space come from?print("Score: " + 5) and read the message. Then fix it with str(5).Cheat sheet: Open to print (A4, with a picture of what each command draws) · Markdown
Turn typed text into numbers, and check it first so a typo does not crash the program.
What input() gives back is always text, even when someone types digits. "5" + "5" is "55". To calculate, turn the text into a number with int("5").
int("twelve") stops the program with an error, so a careful program checks first. text.isdigit() asks the text whether every character is a digit and gives back True or False. The dot means "ask this text". An if uses the answer: convert inside the if, and reply politely in the else.
isdigit() first.else turns it into a helpful one.A door with a lock that asks "is this a key?" before turning.
Where it breaks: isdigit() only checks the shape of the text. 0 is all digits, so it passes even when 0 makes no sense. Checking that a number is sensible is a second question.
a = input("A? "), b = input("B? "), print(a + b) with 5 and 5. Then wrap each input in int(...) and run again.twelve into the int() version and read the error.if text.isdigit(): check around it and run it again with twelve.isdigit() says no (the dot is not a digit) and int() cannot read it. float() can, but this course sticks to whole numbers.Cheat sheet: Open to print (A4, with a picture of what each command draws) · Markdown
Repeat with while, keep a running total, and read a whole guessing game.
A while loop repeats for as long as a question is true: while guess != secret: keeps asking until the guess is right. It asks the question before every lap, so something inside the loop must change the answer, or the loop runs for ever (the playground's Stop button ends it).
+= adds to a variable: total += 5 is short for total = total + 5. Set the total to 0 once, before the loop, and it builds up as the loop goes.
A while loop is "keep stirring while the sauce is lumpy". The cook checks before each stir, and stops when the answer changes.
Where it breaks: if nothing changes the sauce (you forget to stir), the cook checks for ever. In code, a line that asks again must be inside the loop, or the condition never changes.
tries += 1 out of the loop (dedent it) and see what the program says.total = 0 to be inside the loop in the running-total program. What happens?for when you know how many times (four sides of a square); while when you are waiting for something to happen (the right guess).Cheat sheet: Open to print (A4, with a picture of what each command draws) · Markdown
A menu that keeps showing, and test cases written as a table.
A menu is while True:, a loop whose question is always yes, with break as the way out. Each lap shows the choices, asks for one, and runs one branch of an if / elif, with else for anything else. break leaves the whole loop, and the program carries on after it.
A test case is an input and the output you expect, written down before running. If the menu's replies come from a function, each test is one line: print(reply("9") == "Please choose 1 or 2.") shows True or False. A good list of tests includes the wrong inputs.
A café counter: ask what they want, add it to the order, ask again, until they say that is all.
Where it breaks: a person at the counter knows when to stop. A program only stops when a line says so, so a menu with no break is a menu that never ends.
break line, run it, and press Stop. What does that tell you?break. It reads as "keep going until I say stop".Cheat sheet: Open to print (A4, with a picture of what each command draws) · Markdown
Build a console app with a menu, a checked number and a total.
Cheat sheet: Open to print (A4, with a picture of what each command draws) · Markdown