The method returns a new string, so make sure you
assign it to something
Nondeterministic Programming
For Wordle, the game is only interesting if the secret word is not
the same every time!
Let’s look at the built-in random
library, which lets us simulate random processes
Programs that involve random processes that cannot be predicted in
advance are said to be nondeterministic
Nondeterministic behavior is essential to many applications.
Many games would not be enjoyable if they behaved the exact same way
every playthrough
Important practical uses in simulations, computer security, and
algorithm research
Important Functions in
random
Random Integers
randint(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
Random Examples
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
Chapter 4: The Worth of a
Picture
There comes a time when reading and entering text on a terminal
doesn’t cut it
Maybe you need more complicated input
Maybe you need a more complicated interface that pure text can
manage
Maybe you have output that can not be shown as text
Standard Python really only deals with the terminal interface
Lots of outside libraries give Python more visual input/output
Turtle
Matplotlib
Tkinter ← PGL
PyGame
Arcade
The Portable Graphics
Library
Built atop Tkinter
The library (pgl.py) is available on the
Canvas website
Put it in the same folder as your code, and then you can import
it
Operates on the idea of a collage or cork-board
Test
Note that newer objects can obscure older objects. This layering
arrangement is called the stacking order.
The Pieces
At its simplest then, we have two main parts:
The window (or felt-board/cork-board)
Created with the GWindow function
Takes two arguments: a width and a height in pixels
The contents
A wide assortment of shapes and lines that can be added to the
scene
Control over where they are placed, how large they are, what color
they are, etc