← Console bridge: text in, text out

Console bridge: text in, text out: recap

Answer each question in your head or on paper first, then check it.

Text in, text out

1. Print text

Try firstprint("Hi, " + "Sam") shows what, and why is there a space?

Check your answer

Hi, Sam. The space is inside the first quotes. The + adds nothing.

print(...) shows a line of text and moves to a new line. Text goes inside quotes; the quotes are not shown. A name without quotes is a stored value, so print(name) shows what is stored, and print("name") shows the word name.

+ between two pieces of text joins them with nothing in between. A space only appears if it is typed inside the quotes.

print shows text; + joins pieces of text · Sound on for the voiceover
Open the lesson →

2. Ask and store

Try firstWhat is the difference between input("Name? ") on its own and name = input("Name? ")?

Check your answer

Both ask and wait. Only the second stores the answer, so the program can use it.

input("question") shows the question in the console, waits for the person to type and press Enter, and gives back what they typed as text. Store it with name = input(...) so later lines can use it.

Whatever is typed comes back as text, even digits. The next lessons deal with numbers.

input waits for an answer and gives it back · Sound on for the voiceover
Open the lesson →

3. Numbers into text

Try firstscore = 5. Which prints Score: 5, and why: A) print("Score: " + score) B) print("Score: " + str(score))?

Check your answer

B. + will not join text and a number. str makes the number into text.

+ joins text to text, and adds a number to a number, but will not mix them. To join a number onto text, turn it into text first: str(score) gives back a text copy, such as "5". The number itself stays a number.

Calculations can go inside: str(cups * price).

str turns a number into text so + can join it · Sound on for the voiceover
Open the lesson →

Numbers from text

4. Text into numbers

Try firsta = input("?") and the person types 7. What is a + a, and what is int(a) + int(a)?

Check your answer

a + a is 77 (text joined). int(a) + int(a) is 14.

input() always gives back text. int(text) turns text made of digits into a whole number, so + adds instead of joins, and * multiplies instead of repeating text. Convert once, as the answer is stored: age = int(input("Age? ")).

A number joined onto text still needs str, as in the last lesson.

input gives text; int turns it into a number · Sound on for the voiceover
Open the lesson →

5. Check first

Try firstWhy does the check go before int, and what does text.isdigit() give?

Check your answer

int stops the program on text that is not digits, so the program asks first. isdigit gives True or False.

int("twelve") stops the program with an error, so a careful program checks first. text.isdigit() asks the text whether it is all digits and gives back True or False, which an if uses. The dot means "ask this text": the same shape appears for many questions you can ask a piece of text.

The pattern is: store the text, check it, convert inside the if, and reply in the else.

isdigit asks the text if it is all digits · Sound on for the voiceover
Open the lesson →

Keep asking

6. Repeat until

Try firstA while loop's condition is already False the first time it is checked. How many times does the body run?

Check your answer

Zero times. while checks before every pass, including the first.

while condition: checks the condition. If it is true, the indented lines run, and then it checks again. If it is false, the lines are skipped and the program carries on after the loop.

Something inside the loop must be able to change the condition, usually by asking again with input. If nothing changes, the loop never ends.

while repeats for as long as the condition is true · Sound on for the voiceover
Open the lesson →

7. Running total

Try firsttotal is 7. What is total after total += 3, and what was the line short for?

Check your answer

10. total += 3 is short for total = total + 3.

total += n means "add n to what total already holds" and is short for total = total + n. A running total is set to 0 once before the loop, then changed on every pass. If it were set to 0 inside the loop, every pass would start again from nothing.

The loop's condition can read the total, or the loop can wait for a special answer such as 0 to finish.

+= adds to the total on every pass · Sound on for the voiceover
Open the lesson →

8. Guess the number

Try firstWhich line in the guessing game makes it ask again, and which line counts the guesses?

Check your answer

guess = input("Try again: ") asks again, and tries += 1 counts.

A longer program is easier to read once you split it into jobs: set up (the secret and the first guess), the loop (keep asking until the guess is right), the check and hint inside the loop, and the count. Each job uses a move from an earlier lesson.

The secret comes from random.randint(1, 20), which gives a random whole number from 1 to 20. For now it is a recipe to copy.

A longer program is made of parts that each do one job · Sound on for the voiceover
Open the lesson →

Menus and testing

9. A menu that keeps showing

Try firstWhat does break do, and what happens to the program after it?

Check your answer

break leaves the loop at once. The program carries on with the first line after the loop.

while True: is a loop whose condition is never false, so it only ends when a break runs. A menu fits: show the choices, read one, do it, and go round again. if / elif / else picks what to do for each choice, and else catches anything else.

break leaves the loop it is in, not only the if. The program continues with the first line after the loop.

while True keeps going; break is the way out · Sound on for the voiceover
Open the lesson →

10. Test with a table

Try firstWhy should a test list always include a wrong input?

Check your answer

Wrong inputs are where bugs hide. If only the cases that work are tested, a bug in the other cases is never found.

A test case is an input and the reply you expect. print(reply("9") == "Please choose 1 or 2.") calls the function with that input and shows True if the reply is exactly what was expected.

Good tests include normal cases and wrong ones. A test that shows False means either the code or the test is wrong: read both.

A test case is what you give and what you expect back · Sound on for the voiceover
Open the lesson →

Console challenge

11. Challenge: a console app

Try firstName three moves your app uses and the line that shows each.

Check your answer

Any three of: input, int with isdigit, while True with break, +=, if/elif/else, a function.

A bigger app is the small moves working together: a loop keeps the menu going, if / elif / else picks what to do, isdigit checks typed numbers, += builds the total and break ends the loop. A function gives back the replies so they can be tested.

A good plan is the theme, the choices, what is stored, and what ends it.

Open the lesson →