Examples
Each example is a small program you can run and change. Press Try it to open it in the playground.
Greet the person by name
# Greet the person by name
ask "What is your name?" and call it name
if name is empty
set name to "friend"
show "Hello, {name}! Nice to meet you."
ask "How old are you?" and call it age
if age is a number
show "Next year you will be {age plus 1}."
otherwise
show "That's fine. You don't have to say."
show "Goodbye, {name}."
Try itGuess my number
# Guess my number
set secret to a random number from 1 to 100
set tries to 0
show "I'm thinking of a number from 1 to 100."
repeat forever
ask "What's your guess?" and call it guess
if guess is not a whole number
show "Please type a whole number, like 42."
skip to the next round
if guess is less than 1 or more than 100
show "Pick a number from 1 to 100."
skip to the next round
add 1 to tries
if guess is less than secret
show "Higher!"
otherwise if guess is more than secret
show "Lower!"
otherwise
show "You got it in {tries} tries!"
stop repeating
show "Thanks for playing."
Try itFind the big sales
# Find the big sales
read "sales.csv" and call it sales
show "You have {the number of sales} sales."
set big sales to the sales where amount is more than 100
sort big sales by amount, largest first
set total to the total of the amounts of big sales
show "Big sales, largest first:"
repeat for each sale in big sales
show " {sale's customer}: ${sale's amount}"
show "{the number of big sales} big sales, worth ${total} in all."
save big sales to "big_sales.csv"
show "Saved them to big_sales.csv."
Try itLook up a project on GitHub
# Look up a project on GitHub
set address to "https://api.github.com/repos/python/cpython"
show "Asking GitHub about python/cpython..."
try
read address and call it project
show "Name: {project's name}"
show "Stars: {project's stargazers count}"
show "Open issues: {project's open issues count}"
if that fails
show "Sorry, Plain couldn't get the answer from GitHub."
show "{the problem}"
Try itWork out the tips for two meals
# Work out the tips for two meals
set lunch to 42.50
work out the tip on lunch at 15% and call it lunch tip
show "Lunch was ${lunch}. Leave ${lunch tip}."
ask "How much was dinner?" and call it dinner
work out the tip on dinner at 20% and call it dinner tip
show "Dinner was ${dinner}. Leave ${dinner tip}."
show "Tips altogether: ${lunch tip plus dinner tip}"
# How the tip is worked out
how to work out the tip on (bill) at (rate)
set tip to bill times rate
give back tip rounded to 2 places
Try itA to-do list that remembers
# A to-do list that remembers
set todos to an empty list
if the file "todos.txt" exists
read the lines of "todos.txt" and call it todos
repeat forever
ask "What now?" with choices "Add", "Show", "Cross off" and "Quit" and call it choice
if choice is "Add"
ask "What do you need to do?" and call it task
add task to todos
otherwise if choice is "Show"
show todos
otherwise if choice is "Cross off"
cross off a task in todos
otherwise
stop repeating
save todos to "todos.txt"
show "Saved your list. See you next time!"
# The details
how to cross off a task in (tasks)
if tasks is empty
show "There's nothing to cross off."
otherwise
ask "Which one is done?" with choices tasks and call it done
remove done from tasks
Try itAdd up a monthly budget
Read your budget from a spreadsheet, see the total for each category, and save the totals to a new file.
# Add up a monthly budget
# Read your budget from a spreadsheet, see the total for each category, and save the totals to a new file.
# Try: Add a row to budget.csv for a new category, like Gifts, then run it again.
# Try: Fill in the blank amounts in budget.csv and watch the totals change.
# Try: Add the line show summary at the end, to see the totals lined up in a table.
read "budget.csv" and call it budget
set summary to an empty list
show "Your budget, by category:"
repeat for each kind in the different categories of budget
set rows to budget where category is kind
set category total to the total of the amounts of rows rounded to 2 places
show " {kind}: ${category total}"
set entry to a new record
set entry's category to kind
set entry's total to category total
add entry to summary
show "Altogether: ${the total of the amounts of budget rounded to 2 places}"
set missing to budget where amount is empty
if missing is not empty
show "No amount yet for {the items of missing}, so they aren't in the totals."
save summary to "budget_summary.csv"
show "Saved the totals to budget_summary.csv."
Try itThings you could change
- Add a row to budget.csv for a new category, like Gifts, then run it again.
- Fill in the blank amounts in budget.csv and watch the totals change.
- Add the line show summary at the end, to see the totals lined up in a table.
Hangman
Guess a secret word one letter at a time, before you make 6 wrong guesses.
# Hangman
# Guess a secret word one letter at a time, before you make 6 wrong guesses.
# Try: Add your own words to the list.
# Try: Allow 8 wrong guesses instead of 6.
# Try: Before each guess, show the letters you've tried so far.
set words to a list of "garden", "pencil", "rocket", "window", "banana" and "kitten"
set secret to a random item from words
set letters to the characters of secret
set alphabet to the characters of "abcdefghijklmnopqrstuvwxyz"
set right guesses to an empty list
set tried to an empty list
set wrong to 0
set most wrong to 6
show "I'm thinking of a word with {the number of letters} letters."
repeat forever
# Show the word, with _ for each letter you haven't found yet
set shown to an empty list
repeat for each letter in letters
if letter is in right guesses
add letter to shown
otherwise
add "_" to shown
show shown joined with " "
if "_" is not in shown
show "You got it! The word was {secret}."
stop repeating
if wrong is most wrong
show "Out of guesses! The word was {secret}."
stop repeating
ask "Guess a letter:" and call it guess
if guess is not in alphabet
show "Please type one letter, like e."
skip to the next round
if guess is in tried
show "You already tried {guess}."
skip to the next round
add guess to tried
if guess is in letters
add guess to right guesses
show "Yes, {guess} is in the word."
otherwise
add 1 to wrong
show "Sorry, there's no {guess}. Wrong guesses: {wrong} of {most wrong}."
Try itThings you could change
- Add your own words to the list.
- Allow 8 wrong guesses instead of 6.
- Before each guess, show the letters you've tried so far.
Reaction timer
See how fast you can press Enter when Plain says GO.
# Reaction timer
# See how fast you can press Enter when Plain says GO.
# Try: Play 5 rounds instead of 3.
# Try: Make the wait anywhere from 1 to 10 seconds.
# Try: Show "Lightning fast!" when a time is under 0.3 seconds.
set rounds to 3
set results to an empty list
show "Press Enter as soon as you see GO. Don't press it before!"
wait for enter
repeat for each round from 1 to rounds
show "Round {round} of {rounds}. Get ready..."
wait a random number from 2 to 5 seconds
start the timer
ask "GO!"
set reaction to the timer
# No one can react in under 0.1 seconds, so a time that short means you pressed Enter early.
if reaction is less than 0.1
show "Too soon! Wait for GO before you press Enter."
otherwise
show "{reaction} seconds."
add reaction to results
if results is empty
show "No fair rounds this time. Run it again and wait for GO."
otherwise
show "Your best time: {the smallest of results} seconds."
show "Your average: {the average of results rounded to 2 places} seconds."
Try itThings you could change
- Play 5 rounds instead of 3.
- Make the wait anywhere from 1 to 10 seconds.
- Show "Lightning fast!" when a time is under 0.3 seconds.
What's the weather?
Type a city and Plain looks up the temperature there right now.
# What's the weather?
# Type a city and Plain looks up the temperature there right now.
# Try: Show how many people live there too, with place's population.
# Try: Add &temperature_unit=fahrenheit to the end of the forecast address, and change °C to °F.
show "Type a city to see its weather. Press Enter on its own to stop."
repeat forever
ask "Which city?" and call it city
if city is empty
stop repeating
try
read "https://geocoding-api.open-meteo.com/v1/search?name={city}&count=1" and call it places
if places contains "results"
set place to the first item of places's results
read "https://api.open-meteo.com/v1/forecast?latitude={place's latitude}&longitude={place's longitude}¤t=temperature_2m" and call it forecast
show "It's {forecast's current's temperature 2m}°C in {place's name}, {place's country}."
otherwise
show "Plain couldn't find a place called {city}."
if that fails
show "Sorry, Plain couldn't get the weather for {city}."
show the problem
show "Goodbye!"
Try itThings you could change
- Show how many people live there too, with place's population.
- Add &temperature_unit=fahrenheit to the end of the forecast address, and change °C to °F.