Jed Rembold
September 24, 2025
What would be the output of
revq("september", "e") given the definition
below?
def revq(a, b):
c = b.find(a)
return a[ a.find(b)+1 : a.rfind(b) : c+3 ]
"seer""""eptemb""peb"inputWe’ve seen how to display information to a user, but to retrieve
data from a user, we can use Python’s built-in
input() function
The form will generally look like:
|||variable||| = input(|||prompt_text|||)
|||variable||| is the variable name you
want to assign the user’s typed input to|||prompt_text||| is the string that
will be displayed on the screen to communicate to the user what they
should be doingThe input() function always
returns a string
If you want to get an integer from the user, you will need to convert it yourself after retrieving it
num = int(input('Pick a number between 1 and 10: '))Constructing text or sentences by interleaving strings and other objects comes up a lot in communicating code results to a user
For any Python version past 3.6, the nicest and easiest way to do this is with what are called f-strings:
A = 10
print(f"The value of A is: {A}!")You can define an f-string anytime you would normally define a string, just be aware that the substitution happens with the values of variable at that point
A = 10
s = f"The value of A is {A}"
A = 12
print(s){} placeholder
A = 10.234
print(f"The value of A is {A:0.2f}")+ sign ensures all numbers will have
either a + or -
sign in front- sign in front)<, >,
or ^ for left, right, or center
justified| Code | Description |
|---|---|
b |
Inserts an integer using its binary representation |
d |
Inserts an integer using its decimal representation |
e or E |
Inserts a number using scientific notation |
f or F |
Inserts a number using a decimal point format |
g or G |
Choose e or
f to get the best fit |
o |
Inserts an integer using its octal representation |
s |
Inserts a string value |
x or X |
Inserts an integer using its hexadecimal representation |
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(|||seq|||) |
Returns a random element from
|||seq||| |
sample(|||seq|||, |||k|||) |
Returns a list of
|||k||| elements from
|||seq||| |
shuffle(|||seq|||) |
Randomly reorders the elements of mutable
|||seq||| |
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
pgl.py) is available on the
website
GWindow functionfrom pgl import GWindow, GRect
GW_WIDTH = 500
GW_HEIGHT = 200
gw = GWindow(GW_WIDTH, GW_HEIGHT)
rect = GRect(150, 50 ,200, 100)
rect.set_color("Blue")
rect.set_filled(True)
gw.add(rect)
Functions to create simple geometric objects:
GRect( |||x|||, |||y|||, |||width|||, |||height||| )GOval( |||x|||, |||y|||, |||width|||, |||height||| )GLine( |||x1|||, |||y1|||, |||x2|||, |||y2||| )GObject Hierarchy
GObject type represents the
collection of all graphical objectsGFillableObject type represents
those that have a fillable interiorGWindowWe’ve already shown creation:
gw = GWindow(|||width|||, |||height|||)You have several more operations that you can apply to the
GWindow object:
| Method | Description |
|---|---|
|||gw|||.add(|||object|||) |
Adds an object to the window |
|||gw|||.add(|||object|||, |||x|||, |||y|||) |
Adds an object to the window after moving it to (x,y) |
|||gw|||.remove(|||object|||) |
Removes an object from the window |
|||gw|||.get_width() |
Returns the width of the graphics window in pixels |
|||gw|||.get_height() |
Returns the height of the graphics window in pixels |
GObjectsobject is the name of any specific
instance.| Method | Description |
|---|---|
|||object|||.get_x() |
Returns the x coordinate of this object |
|||object|||.get_y() |
Returns the y coordinate of this object |
|||object|||.get_width() |
Returns the width of this object |
|||object|||.get_height() |
Returns the height of this object |
|||object|||.set_color(|||color|||) |
Sets the color of the object to the specified color |
GFillableObjects| Method | Description |
|---|---|
|||object|||.set_filled(|||bool|||) |
Sets the fill state of the object |
|||object|||.set_fill_color(|||color|||) |
Sets the color to be used to fill the interior, otherwise same as the outer line |
|||object|||.get_fill_color() |
Gets the current color used to display the object interior |
|||object|||.is_filled() |
Returns True or False depending on whether the object is currently filled |
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)