Lessons
Here you will find two kinds of code:- Examples and their solutions
- Challenges
Example: Print hello world
This is your first program, find it at the root base, run it with this command pythonw ex_print_hello_world.py

def my_program():
printf('Hello world !!!')
###########################################################################
###########################################################################
# The next lines are needed to run compy, don't mind them,
# but keep them, don't get rid of these lines
###########################################################################
###########################################################################
import compy
import time
# define commands of compy, so IDE's will recognize them
def clear_screen(): pass
def set_bg_color(color): pass
def set_fm_color(color): pass
def printf(to_print='', color=None, stay=False, reverse=False): pass
def xyprintf(x, y, *args): pass
def poke(x, y, code, color = None, reverse=False): pass
def peek(self, x, y): pass
def input(message = '', color=None): return None
def wait_key(): pass
def check_key(): pass
def redefine_commands_and_run(screen):
global clear_screen, set_bg_color, set_fm_color, printf, xyprintf
global poke, peek, input, wait_key, check_key
clear_screen = screen.clear_screen
set_bg_color = screen.set_bg_color
set_fm_color = screen.set_fm_color
printf = screen.printf
xyprintf = screen.xyprintf
poke = screen.poke
peek = screen.peek
input = screen.input
wait_key = screen.wait_key
check_key = screen.check_key
my_program()
if __name__ == '__main__':
compy.run(redefine_commands_and_run)
###########################################################################
###########################################################################
Example: Learn how printf works
Look at the way printf wraps the line at the end of the screen. Also look at how you control the color printed. Take a look at the reversed mode and also how to make the next line to be printed following the current one

Challenge: Draw one line
Can you draw a yellow line in the middle of the screen? (Ana did)To do it you will need to create a new text file, you can call it my_python_program.pyand run it with this command pythonw my_python_program.py

def my_program():
clear_screen()
printf()
printf(' DRAW A LINE', color=17)
printf()
printf(' by Ana', color=19)
printf()
printf()
printf(' ', stay=True)
printf(' ', reverse = True, color = 'light-yellow')
###########################################################################
###########################################################################
# The next lines are needed to run compy, don't mind them,
# but keep them, don't get rid of these lines
###########################################################################
###########################################################################
import compy
import time
# define commands of compy, so IDE's will recognize them
def clear_screen(): pass
def set_bg_color(color): pass
def set_fm_color(color): pass
def printf(to_print='', color=None, stay=False, reverse=False): pass
def xyprintf(x, y, *args): pass
def poke(x, y, code, color = None, reverse=False): pass
def peek(self, x, y): pass
def input(message = '', color=None): return None
def wait_key(): pass
def check_key(): pass
def redefine_commands_and_run(screen):
global clear_screen, set_bg_color, set_fm_color, printf, xyprintf
global poke, peek, input, wait_key, check_key
clear_screen = screen.clear_screen
set_bg_color = screen.set_bg_color
set_fm_color = screen.set_fm_color
printf = screen.printf
xyprintf = screen.xyprintf
poke = screen.poke
peek = screen.peek
input = screen.input
wait_key = screen.wait_key
check_key = screen.check_key
my_program()
if __name__ == '__main__':
compy.run(redefine_commands_and_run)
###########################################################################
###########################################################################
Example: Print text and numbers
Look at the way to print numbers inside a string

def my_program():
clear_screen()
msg = 'Here we show how to print text and numbers.'
printf(msg)
printf()
printf('You first create a string that contains the numbers')
printf('To do that, you need to use braces, a point, and the word "format" ')
printf('Lets print a number inside a text string:')
msg = 'I am {} years old'.format(46)
printf(msg, color='yellow')
printf()
printf('You can also use variables and print text inside text:', color='light-blue')
age = 46
name = 'Juanjo'
msg = 'Hello, mi name is {}, I am {} years old'.format(name, age)
printf(msg, color = 'yellow')
printf()
printf('You can also format the number, the options are many, this one is useful:', color='light-blue')
age_universe = 15000000000
msg = 'No format: The universe is {} years old'.format(age_universe)
printf(msg, color='yellow')
msg = 'Formatted: The universe is {:g} years old'.format(age_universe)
printf(msg)
msg = 'Another format: The universe is {:,} years old'.format(age_universe)
printf(msg)
###########################################################################
###########################################################################
# The next lines are needed to run compy, don't mind them,
# but keep them, don't get rid of these lines
###########################################################################
###########################################################################
import compy
import time
# define commands of compy, so IDE's will recognize them
def clear_screen(): pass
def set_bg_color(color): pass
def set_fm_color(color): pass
def printf(to_print='', color=None, stay=False, reverse=False): pass
def xyprintf(x, y, *args): pass
def poke(x, y, code, color = None, reverse=False): pass
def peek(self, x, y): pass
def input(message = '', color=None): return None
def wait_key(): pass
def check_key(): pass
def redefine_commands_and_run(screen):
global clear_screen, set_bg_color, set_fm_color, printf, xyprintf
global poke, peek, input, wait_key, check_key
clear_screen = screen.clear_screen
set_bg_color = screen.set_bg_color
set_fm_color = screen.set_fm_color
printf = screen.printf
xyprintf = screen.xyprintf
poke = screen.poke
peek = screen.peek
input = screen.input
wait_key = screen.wait_key
check_key = screen.check_key
my_program()
if __name__ == '__main__':
compy.run(redefine_commands_and_run)
###########################################################################
###########################################################################
Example: The command input
The command input asks you for a texts string or a number

def my_program():
clear_screen()
printf('This program shows how "input" works')
name = input('What is your name? ')
printf()
printf('Hello, ', stay = True)
printf(name)
age = input('And how old are you? ')
printf()
printf('Last year you were ', stay = True)
printf(age - 1)
###########################################################################
###########################################################################
# The next lines are needed to run compy, don't mind them,
# but keep them, don't get rid of these lines
###########################################################################
###########################################################################
import compy
import time
# define commands of compy, so IDE's will recognize them
def clear_screen(): pass
def set_bg_color(color): pass
def set_fm_color(color): pass
def printf(to_print='', color=None, stay=False, reverse=False): pass
def xyprintf(x, y, *args): pass
def poke(x, y, code, color = None, reverse=False): pass
def peek(self, x, y): pass
def input(message = '', color=None): return None
def wait_key(): pass
def check_key(): pass
def redefine_commands_and_run(screen):
global clear_screen, set_bg_color, set_fm_color, printf, xyprintf
global poke, peek, input, wait_key, check_key
clear_screen = screen.clear_screen
set_bg_color = screen.set_bg_color
set_fm_color = screen.set_fm_color
printf = screen.printf
xyprintf = screen.xyprintf
poke = screen.poke
peek = screen.peek
input = screen.input
wait_key = screen.wait_key
check_key = screen.check_key
my_program()
if __name__ == '__main__':
compy.run(redefine_commands_and_run)
###########################################################################
###########################################################################
Challenge: Sum two numbers
Can you do that?

def my_program():
clear_screen()
printf()
printf('SUM TWO NUMBERS', color=4)
printf('===============')
printf()
first_value = input("Give me one number: ", color=8)
second_value = input("Give me another number: ")
sum_of_values = first_value + second_value
response = 'The sum of {} and {} is {}'.format(first_value, second_value, sum_of_values)
printf()
printf(response, color=6)
###########################################################################
###########################################################################
# The next lines are needed to run compy, don't mind them,
# but keep them, don't get rid of these lines
###########################################################################
###########################################################################
import compy
import time
# define commands of compy, so IDE's will recognize them
def clear_screen(): pass
def set_bg_color(color): pass
def set_fm_color(color): pass
def printf(to_print='', color=None, stay=False, reverse=False): pass
def xyprintf(x, y, *args): pass
def poke(x, y, code, color = None, reverse=False): pass
def peek(self, x, y): pass
def input(message = '', color=None): return None
def wait_key(): pass
def check_key(): pass
def redefine_commands_and_run(screen):
global clear_screen, set_bg_color, set_fm_color, printf, xyprintf
global poke, peek, input, wait_key, check_key
clear_screen = screen.clear_screen
set_bg_color = screen.set_bg_color
set_fm_color = screen.set_fm_color
printf = screen.printf
xyprintf = screen.xyprintf
poke = screen.poke
peek = screen.peek
input = screen.input
wait_key = screen.wait_key
check_key = screen.check_key
my_program()
if __name__ == '__main__':
compy.run(redefine_commands_and_run)
###########################################################################
###########################################################################
Example: The command if
The command if checks a condition

def my_program():
clear_screen()
name = input('What is your name? ')
if name == 'JUANJO' or name == 'PAULA':
msg = 'Hello {}'.format(name)
elif name == 'ANA' or name == 'MIGUEL':
msg = 'Hello {}'.format(name)
else:
msg = 'I do not recognize you, please forget about me, {}'.format(name)
printf(msg, color=4)
###########################################################################
###########################################################################
# The next lines are needed to run compy, don't mind them,
# but keep them, don't get rid of these lines
###########################################################################
###########################################################################
import compy
import time
# define commands of compy, so IDE's will recognize them
def clear_screen(): pass
def set_bg_color(color): pass
def set_fm_color(color): pass
def printf(to_print='', color=None, stay=False, reverse=False): pass
def xyprintf(x, y, *args): pass
def poke(x, y, code, color = None, reverse=False): pass
def peek(self, x, y): pass
def input(message = '', color=None): return None
def wait_key(): pass
def check_key(): pass
def redefine_commands_and_run(screen):
global clear_screen, set_bg_color, set_fm_color, printf, xyprintf
global poke, peek, input, wait_key, check_key
clear_screen = screen.clear_screen
set_bg_color = screen.set_bg_color
set_fm_color = screen.set_fm_color
printf = screen.printf
xyprintf = screen.xyprintf
poke = screen.poke
peek = screen.peek
input = screen.input
wait_key = screen.wait_key
check_key = screen.check_key
my_program()
if __name__ == '__main__':
compy.run(redefine_commands_and_run)
###########################################################################
###########################################################################
Challenge: Find the minimum of two numbers
Can you do that?

def my_program():
clear_screen()
printf()
printf('FIND THE MINIMUM OF TWO NUMBERS', color=4)
printf('================================')
printf()
first_value = input("Give me one number: ", color=8)
second_value = input("Give me another number: ")
if first_value < second_value:
minimum = first_value
else:
minimum = second_value
response = 'The minumum of {} and {} is {}'.format(first_value, second_value, minimum)
printf()
printf(response, color=6)
###########################################################################
###########################################################################
# The next lines are needed to run compy, don't mind them,
# but keep them, don't get rid of these lines
###########################################################################
###########################################################################
import compy
import time
# define commands of compy, so IDE's will recognize them
def clear_screen(): pass
def set_bg_color(color): pass
def set_fm_color(color): pass
def printf(to_print='', color=None, stay=False, reverse=False): pass
def xyprintf(x, y, *args): pass
def poke(x, y, code, color = None, reverse=False): pass
def peek(self, x, y): pass
def input(message = '', color=None): return None
def wait_key(): pass
def check_key(): pass
def redefine_commands_and_run(screen):
global clear_screen, set_bg_color, set_fm_color, printf, xyprintf
global poke, peek, input, wait_key, check_key
clear_screen = screen.clear_screen
set_bg_color = screen.set_bg_color
set_fm_color = screen.set_fm_color
printf = screen.printf
xyprintf = screen.xyprintf
poke = screen.poke
peek = screen.peek
input = screen.input
wait_key = screen.wait_key
check_key = screen.check_key
my_program()
if __name__ == '__main__':
compy.run(redefine_commands_and_run)
###########################################################################
###########################################################################
Example: The command while
The command while loops until it finds the command break

def my_program():
clear_screen()
printf()
printf('This program shows how "while" works')
printf('It will count from 1 to 10')
count = 0
while True:
count = count + 1
printf(count)
if count == 10:
break
###########################################################################
###########################################################################
# The next lines are needed to run compy, don't mind them,
# but keep them, don't get rid of these lines
###########################################################################
###########################################################################
import compy
import time
# define commands of compy, so IDE's will recognize them
def clear_screen(): pass
def set_bg_color(color): pass
def set_fm_color(color): pass
def printf(to_print='', color=None, stay=False, reverse=False): pass
def xyprintf(x, y, *args): pass
def poke(x, y, code, color = None, reverse=False): pass
def peek(self, x, y): pass
def input(message = '', color=None): return None
def wait_key(): pass
def check_key(): pass
def redefine_commands_and_run(screen):
global clear_screen, set_bg_color, set_fm_color, printf, xyprintf
global poke, peek, input, wait_key, check_key
clear_screen = screen.clear_screen
set_bg_color = screen.set_bg_color
set_fm_color = screen.set_fm_color
printf = screen.printf
xyprintf = screen.xyprintf
poke = screen.poke
peek = screen.peek
input = screen.input
wait_key = screen.wait_key
check_key = screen.check_key
my_program()
if __name__ == '__main__':
compy.run(redefine_commands_and_run)
###########################################################################
###########################################################################
Example: The command sleep
The command sleep stops execution for a given number of seconds

def my_program():
clear_screen()
printf('This program shows how to slow execution')
while True:
seconds = input('How many seconds do you want to wait? (0 to exit) ')
if seconds == 0:
break
time.sleep(seconds)
printf('Bye!!', color='light-red')
###########################################################################
###########################################################################
# The next lines are needed to run compy, don't mind them,
# but keep them, don't get rid of these lines
###########################################################################
###########################################################################
import compy
import time
import datetime as dt
now = dt.datetime.utcnow
import random
# define commands of compy, so IDE's will recognize them
def clear_screen(): pass
def set_bg_color(color): pass
def set_fm_color(color): pass
def printf(to_print='', color=None, stay=False, reverse=False): pass
def xyprintf(x, y, *args): pass
def poke(x, y, code, color = None, reverse=False): pass
def peek(self, x, y): pass
def input(message = '', color=None): return None
def wait_key(): pass
def check_key(): pass
def redefine_commands_and_run(screen):
global clear_screen, set_bg_color, set_fm_color, printf, xyprintf
global poke, peek, input, wait_key, check_key
clear_screen = screen.clear_screen
set_bg_color = screen.set_bg_color
set_fm_color = screen.set_fm_color
printf = screen.printf
xyprintf = screen.xyprintf
poke = screen.poke
peek = screen.peek
input = screen.input
wait_key = screen.wait_key
check_key = screen.check_key
my_program()
if __name__ == '__main__':
compy.run(redefine_commands_and_run)
###########################################################################
###########################################################################
Challenge: Validation using a loop
Can you do that?

def my_program():
clear_screen()
printf()
printf('Enter a number between 1 and 20')
while True:
answer = input('Enter number between 1 and 20: ', color =4)
if answer >= 1 and answer <= 20:
number = answer
break
printf('Good job', color=2)
printf('Bye!!', color='light-red')
###########################################################################
###########################################################################
# The next lines are needed to run compy, don't mind them,
# but keep them, don't get rid of these lines
###########################################################################
###########################################################################
import compy
import time
# define commands of compy, so IDE's will recognize them
def clear_screen(): pass
def set_bg_color(color): pass
def set_fm_color(color): pass
def printf(to_print='', color=None, stay=False, reverse=False): pass
def xyprintf(x, y, *args): pass
def poke(x, y, code, color = None, reverse=False): pass
def peek(self, x, y): pass
def input(message = '', color=None): return None
def wait_key(): pass
def check_key(): pass
def redefine_commands_and_run(screen):
global clear_screen, set_bg_color, set_fm_color, printf, xyprintf
global poke, peek, input, wait_key, check_key
clear_screen = screen.clear_screen
set_bg_color = screen.set_bg_color
set_fm_color = screen.set_fm_color
printf = screen.printf
xyprintf = screen.xyprintf
poke = screen.poke
peek = screen.peek
input = screen.input
wait_key = screen.wait_key
check_key = screen.check_key
my_program()
if __name__ == '__main__':
compy.run(redefine_commands_and_run)
###########################################################################
###########################################################################
Challenge: Sum many values entered by user, using a loop
Can you do that?

def my_program():
clear_screen()
printf()
printf('I will sum every value, until you type the letter N')
sum = 0
while True:
answer = input('Enter number to add:', color =4)
if answer == 'N':
break
sum = sum + answer
printf(sum, color=2)
printf('Bye!!', color='light-red')
###########################################################################
###########################################################################
# The next lines are needed to run compy, don't mind them,
# but keep them, don't get rid of these lines
###########################################################################
###########################################################################
import compy
import time
# define commands of compy, so IDE's will recognize them
def clear_screen(): pass
def set_bg_color(color): pass
def set_fm_color(color): pass
def printf(to_print='', color=None, stay=False, reverse=False): pass
def xyprintf(x, y, *args): pass
def poke(x, y, code, color = None, reverse=False): pass
def peek(self, x, y): pass
def input(message = '', color=None): return None
def wait_key(): pass
def check_key(): pass
def redefine_commands_and_run(screen):
global clear_screen, set_bg_color, set_fm_color, printf, xyprintf
global poke, peek, input, wait_key, check_key
clear_screen = screen.clear_screen
set_bg_color = screen.set_bg_color
set_fm_color = screen.set_fm_color
printf = screen.printf
xyprintf = screen.xyprintf
poke = screen.poke
peek = screen.peek
input = screen.input
wait_key = screen.wait_key
check_key = screen.check_key
my_program()
if __name__ == '__main__':
compy.run(redefine_commands_and_run)
###########################################################################
###########################################################################
Challenge: Find the minimum of many values entered by the user
Can you do that?

def my_program():
clear_screen()
printf()
printf('FIND THE MINIMUM OF MANY NUMBERS', color=4)
printf('================================')
printf()
minimum = input("Give me one number: ", color=8)
while True:
new_value = input("Give me another number (N to stop): ")
if new_value == 'N':
break
if new_value < minimum:
minimum = new_value
response = 'The minimum is {}'.format(minimum)
printf()
printf(response, color=6)
###########################################################################
###########################################################################
# The next lines are needed to run compy, don't mind them,
# but keep them, don't get rid of these lines
###########################################################################
###########################################################################
import compy
import time
# define commands of compy, so IDE's will recognize them
def clear_screen(): pass
def set_bg_color(color): pass
def set_fm_color(color): pass
def printf(to_print='', color=None, stay=False, reverse=False): pass
def xyprintf(x, y, *args): pass
def poke(x, y, code, color = None, reverse=False): pass
def peek(self, x, y): pass
def input(message = '', color=None): return None
def wait_key(): pass
def check_key(): pass
def redefine_commands_and_run(screen):
global clear_screen, set_bg_color, set_fm_color, printf, xyprintf
global poke, peek, input, wait_key, check_key
clear_screen = screen.clear_screen
set_bg_color = screen.set_bg_color
set_fm_color = screen.set_fm_color
printf = screen.printf
xyprintf = screen.xyprintf
poke = screen.poke
peek = screen.peek
input = screen.input
wait_key = screen.wait_key
check_key = screen.check_key
my_program()
if __name__ == '__main__':
compy.run(redefine_commands_and_run)
###########################################################################
###########################################################################
Challenge: Make a long word
Can you do that?

def my_program():
clear_screen()
printf()
printf('MAKE A LONG WORD', color=4)
printf('=================')
printf('Your mother is calling!, She start with your name, Paula')
printf('You come to her when she says Paulaaaaaaaa')
printf(color=6)
word = 'Paula'
while True:
printf(word)
if word == 'Paulaaaaaaaa':
break
word = '{}a'.format(word)
###########################################################################
###########################################################################
# The next lines are needed to run compy, don't mind them,
# but keep them, don't get rid of these lines
###########################################################################
###########################################################################
import compy
import time
# define commands of compy, so IDE's will recognize them
def clear_screen(): pass
def set_bg_color(color): pass
def set_fm_color(color): pass
def printf(to_print='', color=None, stay=False, reverse=False): pass
def xyprintf(x, y, *args): pass
def poke(x, y, code, color = None, reverse=False): pass
def peek(self, x, y): pass
def input(message = '', color=None): return None
def wait_key(): pass
def check_key(): pass
def redefine_commands_and_run(screen):
global clear_screen, set_bg_color, set_fm_color, printf, xyprintf
global poke, peek, input, wait_key, check_key
clear_screen = screen.clear_screen
set_bg_color = screen.set_bg_color
set_fm_color = screen.set_fm_color
printf = screen.printf
xyprintf = screen.xyprintf
poke = screen.poke
peek = screen.peek
input = screen.input
wait_key = screen.wait_key
check_key = screen.check_key
my_program()
if __name__ == '__main__':
compy.run(redefine_commands_and_run)
###########################################################################
###########################################################################
Challenge: Squares table
Can you do that?

def my_program():
clear_screen()
printf()
printf('SQUARES TABLE', color=4)
printf('==============')
number = 0
while True:
result = number ** 2
msg = '{}^2 = {}'.format(number, result)
printf(msg)
time.sleep(0.1)
if number == 20:
break
number = number +1
###########################################################################
###########################################################################
# The next lines are needed to run compy, don't mind them,
# but keep them, don't get rid of these lines
###########################################################################
###########################################################################
import compy
import time
# define commands of compy, so IDE's will recognize them
def clear_screen(): pass
def set_bg_color(color): pass
def set_fm_color(color): pass
def printf(to_print='', color=None, stay=False, reverse=False): pass
def xyprintf(x, y, *args): pass
def poke(x, y, code, color = None, reverse=False): pass
def peek(self, x, y): pass
def input(message = '', color=None): return None
def wait_key(): pass
def check_key(): pass
def redefine_commands_and_run(screen):
global clear_screen, set_bg_color, set_fm_color, printf, xyprintf
global poke, peek, input, wait_key, check_key
clear_screen = screen.clear_screen
set_bg_color = screen.set_bg_color
set_fm_color = screen.set_fm_color
printf = screen.printf
xyprintf = screen.xyprintf
poke = screen.poke
peek = screen.peek
input = screen.input
wait_key = screen.wait_key
check_key = screen.check_key
my_program()
if __name__ == '__main__':
compy.run(redefine_commands_and_run)
###########################################################################
###########################################################################
Challenge: Exponential table
Can you do that?

import wait_time
def my_program():
clear_screen()
printf()
printf('EXPONENTIAL TABLE', color=4)
printf('=================')
number = 0
while True:
result = 2 ** number
msg = '2^{} = {}'.format(number, result)
printf(msg)
wait_time.sleep(0.1)
if number == 20:
break
number = number +1
###########################################################################
###########################################################################
# The next lines are needed to run compy, don't mind them,
# but keep them, don't get rid of these lines
###########################################################################
###########################################################################
import compy
import time
# define commands of compy, so IDE's will recognize them
def clear_screen(): pass
def set_bg_color(color): pass
def set_fm_color(color): pass
def printf(to_print='', color=None, stay=False, reverse=False): pass
def xyprintf(x, y, *args): pass
def poke(x, y, code, color = None, reverse=False): pass
def peek(self, x, y): pass
def input(message = '', color=None): return None
def wait_key(): pass
def check_key(): pass
def redefine_commands_and_run(screen):
global clear_screen, set_bg_color, set_fm_color, printf, xyprintf
global poke, peek, input, wait_key, check_key
clear_screen = screen.clear_screen
set_bg_color = screen.set_bg_color
set_fm_color = screen.set_fm_color
printf = screen.printf
xyprintf = screen.xyprintf
poke = screen.poke
peek = screen.peek
input = screen.input
wait_key = screen.wait_key
check_key = screen.check_key
my_program()
if __name__ == '__main__':
compy.run(redefine_commands_and_run)
###########################################################################
###########################################################################
Challenge: Multiplication table
Can you do that?

def my_program():
clear_screen()
printf()
printf('MAKE A MULTIPLICATION TABLE', color=4)
printf('===========================')
multiplier = input("Give me one number: ", color=8)
m2 = 0
while True:
result = multiplier * m2
msg = '{} x {} = {}'.format(multiplier, m2, result)
printf(msg)
if m2 == 12:
break
m2 = m2 +1
###########################################################################
###########################################################################
# The next lines are needed to run compy, don't mind them,
# but keep them, don't get rid of these lines
###########################################################################
###########################################################################
import compy
import time
# define commands of compy, so IDE's will recognize them
def clear_screen(): pass
def set_bg_color(color): pass
def set_fm_color(color): pass
def printf(to_print='', color=None, stay=False, reverse=False): pass
def xyprintf(x, y, *args): pass
def poke(x, y, code, color = None, reverse=False): pass
def peek(self, x, y): pass
def input(message = '', color=None): return None
def wait_key(): pass
def check_key(): pass
def redefine_commands_and_run(screen):
global clear_screen, set_bg_color, set_fm_color, printf, xyprintf
global poke, peek, input, wait_key, check_key
clear_screen = screen.clear_screen
set_bg_color = screen.set_bg_color
set_fm_color = screen.set_fm_color
printf = screen.printf
xyprintf = screen.xyprintf
poke = screen.poke
peek = screen.peek
input = screen.input
wait_key = screen.wait_key
check_key = screen.check_key
my_program()
if __name__ == '__main__':
compy.run(redefine_commands_and_run)
###########################################################################
###########################################################################
Example: Generating random numbers
Look at the way to generate random numbers in python

def my_program():
clear_screen()
printf('This program shows how to generate random numbers')
while True:
answer = input('Do you want a random FLOAT number between 0 and 1? ', color='yellow')
if answer == 'N':
break
number = random.random()
printf(number, color='light-green')
while True:
answer = input('Do you want a random INTEGER number between 0 and 10? ', color='yellow')
if answer == 'N':
break
number = random.randint(0,10)
printf(number, color='light-green')
printf('Bye!!', color='light-red')
###########################################################################
###########################################################################
# The next lines are needed to run compy, don't mind them,
# but keep them, don't get rid of these lines
###########################################################################
###########################################################################
import compy
import time
import datetime as dt
now = dt.datetime.utcnow
import random
# define commands of compy, so IDE's will recognize them
def clear_screen(): pass
def set_bg_color(color): pass
def set_fm_color(color): pass
def printf(to_print='', color=None, stay=False, reverse=False): pass
def xyprintf(x, y, *args): pass
def poke(x, y, code, color = None, reverse=False): pass
def peek(self, x, y): pass
def input(message = '', color=None): return None
def wait_key(): pass
def check_key(): pass
def redefine_commands_and_run(screen):
global clear_screen, set_bg_color, set_fm_color, printf, xyprintf
global poke, peek, input, wait_key, check_key
clear_screen = screen.clear_screen
set_bg_color = screen.set_bg_color
set_fm_color = screen.set_fm_color
printf = screen.printf
xyprintf = screen.xyprintf
poke = screen.poke
peek = screen.peek
input = screen.input
wait_key = screen.wait_key
check_key = screen.check_key
my_program()
if __name__ == '__main__':
compy.run(redefine_commands_and_run)
###########################################################################
###########################################################################
Challenge: Guess the number
Can you do that?

# This is a guess the number game.
import random
def main(sc):
sc.printf('Well, I am thinking of a number between 1 and 20.')
sc.printf()
number = random.randint(1, 20)
num_guesses_taken = 0
while True:
guess = sc.input('Take a guess: ')
guess = int(guess)
num_guesses_taken = num_guesses_taken + 1
if guess < number:
sc.printf('Your guess is too low.')
elif guess > number:
sc.printf('Your guess is too high.')
elif guess == number:
sc.printf()
msg = 'Good job! You guessed my number in {} guesses!'.format(num_guesses_taken)
sc.printf(msg)
break
else:
pass # Don't do anything
sc.printf()
if num_guesses_taken == 5:
msg='Nope. The number I was thinking of was {}'.format(number)
sc.printf(msg)
break
###########################################################################
###########################################################################
# The next lines are needed to run compy, don't mind them,
# but keep them, don't get rid of these lines
###########################################################################
###########################################################################
import compy
import time
import datetime as dt
now = dt.datetime.utcnow
# define commands of compy, so IDE's will recognize them
def clear_screen(): pass
def set_bg_color(color): pass
def set_fm_color(color): pass
def printf(to_print='', color=None, stay=False, reverse=False): pass
def xyprintf(x, y, *args): pass
def poke(x, y, code, color = None, reverse=False): pass
def peek(self, x, y): pass
def input(message = '', color=None): return None
def wait_key(): pass
def check_key(): pass
def redefine_commands_and_run(screen):
global clear_screen, set_bg_color, set_fm_color, printf, xyprintf
global poke, peek, input, wait_key, check_key
clear_screen = screen.clear_screen
set_bg_color = screen.set_bg_color
set_fm_color = screen.set_fm_color
printf = screen.printf
xyprintf = screen.xyprintf
poke = screen.poke
peek = screen.peek
input = screen.input
wait_key = screen.wait_key
check_key = screen.check_key
my_program()
if __name__ == '__main__':
compy.run(redefine_commands_and_run)
###########################################################################
###########################################################################
Example: The command def
The command def defines new commands

def my_program():
clear_screen()
printf()
printf('This program shows how "def" works')
first, second = ask_for_two_numbers()
sum_of_numbers = sum_two_numbers(first, second)
print_response(first, second, sum_of_numbers)
printf('Bye!!', color='light-red')
# This function doesn't receive values, but returns two values
def ask_for_two_numbers():
n1 = input('Give me a number ', color='light-green')
n2 = input('Give me another number ', color='light-green')
return n1, n2
# This function receives two values and returns one
def sum_two_numbers(n1, n2):
sum = n1 + n2
return sum
# This function receives three values, but doesn't return any
def print_response(n1, n2, sum):
response = 'The sum of {} and {} is {}'.format(n1, n2, sum)
printf(response)
###########################################################################
###########################################################################
# The next lines are needed to run compy, don't mind them,
# but keep them, don't get rid of these lines
###########################################################################
###########################################################################
import compy
import time
# define commands of compy, so IDE's will recognize them
def clear_screen(): pass
def set_bg_color(color): pass
def set_fm_color(color): pass
def printf(to_print='', color=None, stay=False, reverse=False): pass
def xyprintf(x, y, *args): pass
def poke(x, y, code, color = None, reverse=False): pass
def peek(self, x, y): pass
def input(message = '', color=None): return None
def wait_key(): pass
def check_key(): pass
def redefine_commands_and_run(screen):
global clear_screen, set_bg_color, set_fm_color, printf, xyprintf
global poke, peek, input, wait_key, check_key
clear_screen = screen.clear_screen
set_bg_color = screen.set_bg_color
set_fm_color = screen.set_fm_color
printf = screen.printf
xyprintf = screen.xyprintf
poke = screen.poke
peek = screen.peek
input = screen.input
wait_key = screen.wait_key
check_key = screen.check_key
my_program()
if __name__ == '__main__':
compy.run(redefine_commands_and_run)
###########################################################################
###########################################################################
Challenge: Draw a square
Can you do that? Use the def word.

def my_program():
clear_screen()
printf()
printf(' DRAW A SQUARE', color=17)
printf()
printf()
n=0
while True:
n = n + 1
draw_line()
if n == 10:
break
def draw_line():
printf(' ', stay=True)
printf(' ', reverse=True, color='light-yellow')
###########################################################################
###########################################################################
# The next lines are needed to run compy, don't mind them,
# but keep them, don't get rid of these lines
###########################################################################
###########################################################################
import compy
import time
# define commands of compy, so IDE's will recognize them
def clear_screen(): pass
def set_bg_color(color): pass
def set_fm_color(color): pass
def printf(to_print='', color=None, stay=False, reverse=False): pass
def xyprintf(x, y, *args): pass
def poke(x, y, code, color = None, reverse=False): pass
def peek(self, x, y): pass
def input(message = '', color=None): return None
def wait_key(): pass
def check_key(): pass
def redefine_commands_and_run(screen):
global clear_screen, set_bg_color, set_fm_color, printf, xyprintf
global poke, peek, input, wait_key, check_key
clear_screen = screen.clear_screen
set_bg_color = screen.set_bg_color
set_fm_color = screen.set_fm_color
printf = screen.printf
xyprintf = screen.xyprintf
poke = screen.poke
peek = screen.peek
input = screen.input
wait_key = screen.wait_key
check_key = screen.check_key
my_program()
if __name__ == '__main__':
compy.run(redefine_commands_and_run)
###########################################################################
###########################################################################
Challenge: Draw a right pointing ladder
Can you do that? Use the def word.

def my_program():
clear_screen()
printf()
printf(' DRAW A LADDER POINTING RIGHT', color=17)
printf()
printf()
printf()
n=0
while True:
n = n + 1
draw_line(n)
if n == 10:
break
def draw_line(n):
printf(' ', stay=True)
printf(' '*n, reverse=True, color='light-yellow')
###########################################################################
###########################################################################
# The next lines are needed to run compy, don't mind them,
# but keep them, don't get rid of these lines
###########################################################################
###########################################################################
import compy
import time
# define commands of compy, so IDE's will recognize them
def clear_screen(): pass
def set_bg_color(color): pass
def set_fm_color(color): pass
def printf(to_print='', color=None, stay=False, reverse=False): pass
def xyprintf(x, y, *args): pass
def poke(x, y, code, color = None, reverse=False): pass
def peek(self, x, y): pass
def input(message = '', color=None): return None
def wait_key(): pass
def check_key(): pass
def redefine_commands_and_run(screen):
global clear_screen, set_bg_color, set_fm_color, printf, xyprintf
global poke, peek, input, wait_key, check_key
clear_screen = screen.clear_screen
set_bg_color = screen.set_bg_color
set_fm_color = screen.set_fm_color
printf = screen.printf
xyprintf = screen.xyprintf
poke = screen.poke
peek = screen.peek
input = screen.input
wait_key = screen.wait_key
check_key = screen.check_key
my_program()
if __name__ == '__main__':
compy.run(redefine_commands_and_run)
###########################################################################
###########################################################################
Challenge: Draw a left pointing ladder
Can you do that? Use the def word.

def my_program():
clear_screen()
printf()
printf(' DRAW A LADDER POINTING LEFT', color=17)
printf()
printf()
printf()
n=0
while True:
n = n + 1
draw_line(n)
if n == 10:
break
def draw_line(n):
printf(' ', stay=True)
space = 11 -n
printf(' '*space, stay=True)
printf(' '*n, reverse=True, color='light-yellow')
###########################################################################
###########################################################################
# The next lines are needed to run compy, don't mind them,
# but keep them, don't get rid of these lines
###########################################################################
###########################################################################
import compy
import time
# define commands of compy, so IDE's will recognize them
def clear_screen(): pass
def set_bg_color(color): pass
def set_fm_color(color): pass
def printf(to_print='', color=None, stay=False, reverse=False): pass
def xyprintf(x, y, *args): pass
def poke(x, y, code, color = None, reverse=False): pass
def peek(self, x, y): pass
def input(message = '', color=None): return None
def wait_key(): pass
def check_key(): pass
def redefine_commands_and_run(screen):
global clear_screen, set_bg_color, set_fm_color, printf, xyprintf
global poke, peek, input, wait_key, check_key
clear_screen = screen.clear_screen
set_bg_color = screen.set_bg_color
set_fm_color = screen.set_fm_color
printf = screen.printf
xyprintf = screen.xyprintf
poke = screen.poke
peek = screen.peek
input = screen.input
wait_key = screen.wait_key
check_key = screen.check_key
my_program()
if __name__ == '__main__':
compy.run(redefine_commands_and_run)
###########################################################################
###########################################################################
Challenge: Draw a pyramid
Can you do that? Use the def word.

def my_program():
clear_screen()
printf()
printf(' DRAW A PYRAMID', color=17)
printf()
printf()
printf()
n=0
while True:
n = n + 1
draw_line(n)
if n == 9:
break
def draw_line(n):
printf(' ', stay=True)
spaces=10-n
printf(' '*spaces, stay=True)
printf(' '*n*2, reverse=True, color='light-yellow')
###########################################################################
###########################################################################
# The next lines are needed to run compy, don't mind them,
# but keep them, don't get rid of these lines
###########################################################################
###########################################################################
import compy
import time
# define commands of compy, so IDE's will recognize them
def clear_screen(): pass
def set_bg_color(color): pass
def set_fm_color(color): pass
def printf(to_print='', color=None, stay=False, reverse=False): pass
def xyprintf(x, y, *args): pass
def poke(x, y, code, color = None, reverse=False): pass
def peek(self, x, y): pass
def input(message = '', color=None): return None
def wait_key(): pass
def check_key(): pass
def redefine_commands_and_run(screen):
global clear_screen, set_bg_color, set_fm_color, printf, xyprintf
global poke, peek, input, wait_key, check_key
clear_screen = screen.clear_screen
set_bg_color = screen.set_bg_color
set_fm_color = screen.set_fm_color
printf = screen.printf
xyprintf = screen.xyprintf
poke = screen.poke
peek = screen.peek
input = screen.input
wait_key = screen.wait_key
check_key = screen.check_key
my_program()
if __name__ == '__main__':
compy.run(redefine_commands_and_run)
###########################################################################
###########################################################################
Example: Start printing in a specific place: xyprint
xyprintf prints in a fixed position

def my_program():
clear_screen()
xyprintf(5, 5, 'This is how xyprintf prints')
xyprintf(0, 0, 'Origin is at the bottom left')
xyprintf(13, 20, "Text doesn't wrap at the end of the line ")
printf(color='light-yellow')
xyprintf(1, 23, 'To print in colors, use a trick')
###########################################################################
###########################################################################
# The next lines are needed to run compy, don't mind them,
# but keep them, don't get rid of these lines
###########################################################################
###########################################################################
import compy
import time
# define commands of compy, so IDE's will recognize them
def clear_screen(): pass
def set_bg_color(color): pass
def set_fm_color(color): pass
def printf(to_print='', color=None, stay=False, reverse=False): pass
def xyprintf(x, y, *args): pass
def poke(x, y, code, color = None, reverse=False): pass
def peek(self, x, y): pass
def input(message = '', color=None): return None
def wait_key(): pass
def check_key(): pass
def redefine_commands_and_run(screen):
global clear_screen, set_bg_color, set_fm_color, printf, xyprintf
global poke, peek, input, wait_key, check_key
clear_screen = screen.clear_screen
set_bg_color = screen.set_bg_color
set_fm_color = screen.set_fm_color
printf = screen.printf
xyprintf = screen.xyprintf
poke = screen.poke
peek = screen.peek
input = screen.input
wait_key = screen.wait_key
check_key = screen.check_key
my_program()
if __name__ == '__main__':
compy.run(redefine_commands_and_run)
###########################################################################
###########################################################################
Example: Use the date and time
Look at the way to obtain the current date and time

def my_program():
clear_screen()
printf('This program shows how to learn time and date')
time = now()
printf(color='yellow')
printf('The time now is {}:{}'.format(time.hour, time.minute))
printf()
printf('Today is the day {} of the month {} of the year {}'.format(time.day, time.month, time.year))
printf()
printf('Bye!!', color='light-red')
###########################################################################
###########################################################################
# The next lines are needed to run compy, don't mind them,
# but keep them, don't get rid of these lines
###########################################################################
###########################################################################
import compy
import time
import datetime as dt
now = dt.datetime.now
import random
# define commands of compy, so IDE's will recognize them
def clear_screen(): pass
def set_bg_color(color): pass
def set_fm_color(color): pass
def printf(to_print='', color=None, stay=False, reverse=False): pass
def xyprintf(x, y, *args): pass
def poke(x, y, code, color = None, reverse=False): pass
def peek(self, x, y): pass
def input(message = '', color=None): return None
def wait_key(): pass
def check_key(): pass
def redefine_commands_and_run(screen):
global clear_screen, set_bg_color, set_fm_color, printf, xyprintf
global poke, peek, input, wait_key, check_key
clear_screen = screen.clear_screen
set_bg_color = screen.set_bg_color
set_fm_color = screen.set_fm_color
printf = screen.printf
xyprintf = screen.xyprintf
poke = screen.poke
peek = screen.peek
input = screen.input
wait_key = screen.wait_key
check_key = screen.check_key
my_program()
if __name__ == '__main__':
compy.run(redefine_commands_and_run)
###########################################################################
###########################################################################