← Console bridge: text in, text out

Python console reference

The commands from the console bridge, with what each one does.

Print a cheat sheet (the whole course or one unit, with a picture of what each command draws)

Code structure

for i in range(4):
    pass

Example

for i in range(5):
    forward(15)
    penup()
    forward(10)
    pendown()
for loop

Repeat the indented lines this many times.

What the example draws
range(5): five dashes

id: for-loop

while True:
    pass

Example

r = 10
while r < 60:
    circle(r)
    r = r + 15
while loop

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

What the example draws
A circle each time, while r < 60

id: while-loop

name = input("What is your name? ")
print("Hello, " + name)

Example

name = input("What is your name? ")
print("Hello, " + name)
ask and reply

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

What the example draws
Asks for a name, then says hello

id: ask-and-reply

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.")
check then convert

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

What the example draws
A checked ticket total

id: check-then-convert

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

Example

total = 0
while total < 10:
    total += int(input("Add: "))
    print("Total: " + str(total))
print("Done")
running total

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

id: running-total

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

Example

total = 0
while True:
    choice = input("1 add $5, 2 pay: ")
    if choice == "1":
        total += 5
    elif choice == "2":
        break
    else:
        print("Please choose 1 or 2.")
print("Pay $" + str(total))
menu loop

A menu that keeps showing until a break runs.

id: menu-loop

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()
if / else

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

What the example draws
Red when x > 0, else blue

id: if-else

for i in range(8):
    if i % 2 == 0:
        pass
    else:
        pass

Example

for i in range(8):
    if i % 2 == 0:
        pencolor("red")
    else:
        pencolor("blue")
    pensize(6)
    forward(30)
    right(45)
every other

i % 2 == 0 is true on every second pass.

What the example draws
Red and blue sides, taking turns

id: every-other

size = 50

Example

length = 120
forward(length)
left(90)
forward(length / 2)
variable

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

What the example draws
length = 120, then half of it

id: variable

sides = 5
for i in range(sides):
    forward(60)
    right(360 / sides)

Example

sides = 6
for i in range(sides):
    forward(60)
    right(360 / sides)
named turn

Name the number of sides once; the turn is worked out from it.

What the example draws
A hexagon: change sides and the turn follows

id: named-turn

for i in range(20):
    forward(i * 5)
    right(90)

Example

for i in range(30):
    forward(i * 5)
    right(90)
counter step

Use the counter i to make each pass different.

What the example draws
A square spiral: each line longer than the last

id: counter-step

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
grow value

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

What the example draws
The same spiral, grown by an update

id: grow-value

colours = ["red", "blue", "green"]
list

Keep many values in order under one name. The first item is place 0.

id: list

for c in colours:
    pass

Example

pensize(6)
for c in ["red", "blue", "green"]:
    pencolor(c)
    forward(40)
for each item

Work through a list, one item at a time.

What the example draws
One line per colour in the list

id: for-each-item

def shape():
    pass

shape()

Example

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

zigzag()
zigzag()
zigzag()
def (function)

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

What the example draws
Defined once, called three times

id: def-function

print("Hello")
print

Show text or a value in the output box.

id: print

import random

n = random.randint(1, 10)
random number

Pick a random whole number between two values.

id: random-number

Text in and out

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

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

id: input

"Hello, " + name
join text

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

id: join-text

str(score)
str

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

id: str

int(text)
int

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

id: int

text.isdigit()
isdigit

True if the text is all digits (0 to 9) and not empty, so int() will work on it.

id: isdigit

total += 1
add to (+=)

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

id: add-to

break
break

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

id: break