← Reference

Console bridge: text in, text out: cheat sheet

Console bridge: text in, text out · In any playground, open Reference to copy these.

Text in, text out

ask and reply

Ask a question, store the typed answer, and use it in a reply.

name = input("What is your name? ")
print("Hello, " + name)
What it draws
Asks for a name, then says hello

variable

Give a value a name, so you can reuse and change it.

size = 50
Example
length = 120
forward(length)
left(90)
forward(length / 2)
What it draws
length = 120, then half of it

print

Show text or a value in the output box.

print("Hello")

input

Show a question in the console, wait for an answer, and store what was typed. It is always text.

name = input("What is your name? ")

join text

Join pieces of text into one with +. Spaces only appear where they are inside the quotes.

"Hello, " + name

check then convert

Check typed text with isdigit before turning it into a number.

text = input("How many? ")
if text.isdigit():
    number = int(text)
    pass
else:
    print("Please type a number.")
Example
text = input("How many tickets? ")
if text.isdigit():
    print("That will be $" + str(int(text) * 12) + ".")
else:
    print("Please type a number.")
What it draws
A checked ticket total

grow value

Change a value every pass: read the old one, store a new one.

size = 5
for i in range(20):
    forward(size)
    right(90)
    size = size + 5
Example
size = 5
for i in range(30):
    forward(size)
    right(90)
    size = size + 5
What it draws
The same spiral, grown by an update

str

Turn a number into text, so it can be joined: "Score: " + str(score).

str(score)

Numbers from text

if / else

Do one thing or the other, depending on a condition.

if x > 0:
    pass
else:
    pass
Example
for x in [-90, -30, 30, 90]:
    penup()
    goto(x, 0)
    if x > 0:
        dot(30, "red")
    else:
        dot(30, "blue")
hideturtle()
What it draws
Red when x > 0, else blue

int

Turn text made of digits, such as "12", into a whole number you can calculate with.

int(text)

Keep asking

while loop

Repeat the indented lines for as long as the condition is true.

while True:
    pass
Example
r = 10
while r < 60:
    circle(r)
    r = r + 15
What it draws
A circle each time, while r < 60

add to (+=)

Add to a variable: total += 5 is short for total = total + 5.

total += 1

running total

Start the total at 0 once, then add to it on every pass.

total = 0
while total < 10:
    total += int(input("Add: "))

random number

Pick a random whole number between two values.

import random

n = random.randint(1, 10)

Menus and testing

menu loop

A menu that keeps showing until a break runs.

while True:
    choice = input("1 add, 2 quit: ")
    if choice == "1":
        pass
    elif choice == "2":
        break
    else:
        print("Please choose 1 or 2.")

break

Leave the loop straight away; the program carries on after it.

break

def (function)

Name a group of steps, then run it by calling its name.

def shape():
    pass

shape()
Example
def zigzag():
    left(45)
    forward(30)
    right(90)
    forward(30)
    left(45)

zigzag()
zigzag()
zigzag()
What it draws
Defined once, called three times