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)
Console bridge: text in, text out · In any playground, open Reference to copy these.
Ask a question, store the typed answer, and use it in a reply.
name = input("What is your name? ")
print("Hello, " + name)
Give a value a name, so you can reuse and change it.
size = 50length = 120
forward(length)
left(90)
forward(length / 2)
Show text or a value in the output box.
print("Hello")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 pieces of text into one with +. Spaces only appear where they are inside the quotes.
"Hello, " + nameCheck 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.")text = input("How many tickets? ")
if text.isdigit():
print("That will be $" + str(int(text) * 12) + ".")
else:
print("Please type a number.")
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 + 5size = 5
for i in range(30):
forward(size)
right(90)
size = size + 5
Turn a number into text, so it can be joined: "Score: " + str(score).
str(score)Do one thing or the other, depending on a condition.
if x > 0:
pass
else:
passfor x in [-90, -30, 30, 90]:
penup()
goto(x, 0)
if x > 0:
dot(30, "red")
else:
dot(30, "blue")
hideturtle()
Turn text made of digits, such as "12", into a whole number you can calculate with.
int(text)Repeat the indented lines for as long as the condition is true.
while True:
passr = 10
while r < 60:
circle(r)
r = r + 15
Add to a variable: total += 5 is short for total = total + 5.
total += 1Start the total at 0 once, then add to it on every pass.
total = 0
while total < 10:
total += int(input("Add: "))Pick a random whole number between two values.
import random
n = random.randint(1, 10)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.")Leave the loop straight away; the program carries on after it.
breakName a group of steps, then run it by calling its name.
def shape():
pass
shape()def zigzag():
left(45)
forward(30)
right(90)
forward(30)
left(45)
zigzag()
zigzag()
zigzag()