Jed Rembold
February 10, 2023
gw = GWindow(400, 400)
head = GOval(20, 20, 360, 360)
head.set_fill_color("yellow")
head.set_filled(True)
gw.add(head)
reye = GOval(110, 100, 40, 40)
reye.set_filled(True)
gw.add(reye)
leye = GOval(250, 100, 40, 40)
leye.set_filled(True)
gw.add(leye)
mouth = GLine(150, 250, 250, 250)
mouth.set_line_width(5)
gw.add(mouth)
Which of the below images would be produced by the following code?
gw = GWindow(200,200)
for c in range(0,10):
for r in range(0,10):
rect = GRect(20*c,20*r,20,20)
if (r+c) % 2 != 0:
rect.set_filled(True)
gw.add(rect)
Sometimes you need to add some text to the window
Can display any string using GLabel
using the following format:
msg = GLabel(string_to_add, x_location, y_location)Here string_to_add is the text you
want to display, and x_location and
y_location are the (x,y) coordinates of
where you want to place the origin of the label.
GLabel class relies on some
geometrical concepts that are derived from classical typesetting
GLabel has several special methods
that you can use to interact with it
You can use: get_width(),
get_height(),
get_ascent(), and
get_descent() methods to obtain the
geometric properties
You can set a special font for the label using
labelname.set_font(font)Where font is a string comprised of
the following elements:
italicboldpt,
px, or em)serif,
sans-serif, or
monospace) to ensure that the label can
displaygw = GWindow(500, 200)
msg = GLabel("hello world!", 50, 100)
msg.set_font("italic bold 80px 'times new roman'")
gw.add(msg)
GLabelGLabel without
setting its location.set_font()
method to set the desired font (which could change the size)GLabel at the
newly calculated positiongw = GWindow(500, 200)
msg = GLabel("hello world!")
msg.set_font("italic bold 20px 'times new roman'")
x = gw.get_width() / 2 - msg.get_width() / 2
y = gw.get_height() / 2 + msg.get_ascent() / 2
gw.add(msg, x, y)
random
library, which lets us simulate random processesrandomrandint(minv, maxv) |
Returns an integer between minv and maxv, inclusive |
randrange(limit) |
Returns an integer from 0 up to but not including limit |
randrange(start,limit) |
Returns an integer from start up to but not including limit |
random() |
Returns a random float between 0 and 1 |
uniform(minv, maxv) |
Returns a random float between minv and maxv |
choice(a_list) |
Returns a random element from
a_list |
sample(a_list, k) |
Returns a list of
k elements from
a_list |
shuffle(a_list) |
Randomly reorders the elements of
a_list |
import random
def random_redblue():
if random.random() > 0.5:
return "red"
else:
return "blue"
def random_color():
color_string = "#"
for i in range(6):
color_string += random.choice("0123456789ABCDEF")
return color
def function_name( parameter_list ):
function_body
Functions that return a Boolean value are called predicate functions
def is_divisible_by(x, y):
return x % y == 0Once you have defined a predicate function, you can use it in any conditional expression!
for i in range(1, 100):
if is_divisible_by(i, 7):
print(i)Don’t complicate your code for no reason!
Work directly with the boolean values when possible
Try not to code patterns like the following:
def is_divisible_by(x, y):
if x % y == 0:
return True
else:
return False
for i in range(1, 100):
if is_divisible_by(i,7) == True:
print(i)So far we have used a positional way to assign arguments to parameters
>>> def func( first, second, third ):
print( first, second, third )
>>> func(1,2,3)
1 2 3
>>> func(2,6,4)
2 6 4
Arguments may also be specified by a keyword, in which the caller precedes the argument with a parameter name and equals sign
Always stores the argument value in the specified parameter
>>> def func( first, second, third ):
print( first, second, third )
>>> func(third=4, first=2, second=6)
2 6 4Keyword arguments can appear in any order
All keyword arguments must come after any positional arguments!