import

Calvin (Deutschbein)

W5Wed: 25 Sep


Announcements

  • By next Monday 11:59 PM: "Project 1: Wordle"
    • By Tue: Display a word
    • By Wed: Check a guess
    • By Thr: Color letters
    • By Fri: Random word
    • By Sat: Change rows
    • By Sun: Color keys.
  • Today and Tomorrow: Sections.

Announcements

  • Our thinking is developing well but I think we are struggling with attention to do detail
  • I will no longer be accepting:
    • Code that does not run
    • Questions related to indentation.
  • You have now had a chance to submit 9 .py files for feedback (which I feel like I have done a pretty good job of providing?).
  • Correct indentation will be necessary on the forthcoming midterm.
    • Basically: I am out of runway to grade leniently when code isn't correct.

Indentation

  • These lines of code are different: def print_first_vowel(s:str): for c in s: if c in "aeiou": print(c) return def print_all_vowels(s:str): for c in s: if c in "aeiou": print(c) return

Indentation

  • This code doesn't run, isn't commented, and it's not clear what it is intended to do. def vowel_print(s:str): for c in s: if c in "aeiou": print(c) return
  • Ambigious name, no comments, ambigious code... No way to for me to fix this.

Indentation

  • I am still seeing a bit of: for c in s: # line with a def, if, for, while ending with a : print(c) # line at the same indentation level
  • This is never correct and was the subject of a week 1 lecture.
  • My appeal:
    • As a rule, we are expected to place spaces in the correct places in our sentences.
    • A sar ule wear eexp ected top laces pace sin thec or rec tp lace sin our sent ences.

Projects

  • As we advance to working on projects, after a series of (wildly successful) projects:
    • As a class, we now focus on "how to solve problems".
    • Typos are fine to make, but you should know how to solve the problem of typos.
    • Look back at e.g. the "debug" lecture for a systematic way.
  • We want to discuss: "How can we combine 'for' and 'if' statements to solve this problem?"
  • We don't want: "Should this line end with a colon?"

Today

  • String patterns
  • String formats
  • Randomization
  • Graphics

Methods to find string patterns

    Method Description
    string.find(pattern) Returns the first index of 'pattern' in 'string', or '-1' if it does not appear
    string.find(pattern, k) Same as the one-argument version, but starts searching at index 'k
    string.rfind(pattern) Returns the last index of 'pattern' is 'string', or '-1' if missing
    string.rfind(pattern, k) Same as the one-argument version, but searches backwards from index 'k
    string.startswith(prefix) Returns 'True' if the string starts with 'prefix
    string.endswith(suffix) Returns 'True' if the string ends with 'suffix

Some examples

  • I never use these haha. >>> str = "hello world" >>> >>> str.rfind('o') 7 >>> str.rfind('lo') 3 >>> str.find('o') 4 >>> str.find('l') 2 >>> str.rfind('l') 9 >>> str.startswith("he") True >>> str.startswith("hl") False >>> str.endswith("lo") False >>> str.endswith("ld") True >>>

Random

  • For Wordle, the game is only interesting if the secret word is not the same every time!
  • Let’s take a moment to look at the built-inlibrary, which lets us simulate random processes
  • import random
  • Like "import karel" basically, but with an important difference:
  • We either must prefix with "random." >>> import random >>> random.random() # will be between 0 and 1 0.14444921034146263

Random

  • We either must prefix with "random." >>> import random >>> random.random() # will be between 0 and 1 0.14444921034146263
  • Or say "from random import *" >>> from random import * >>> random() # don't need prefix with 'from _ import *' >>> random() # don't need prefix with 'from _ import *' 0.1254407334152583

import random

    random.randint(minv, maxv) Returns an integer between minv and maxv, inclusive
    random.randrange(limit) Returns an integer from 0 up to but not including limit
    random.randrange(start,limit) Returns an integer from start up to but not including limit
    random.random() Returns a random float between 0 and 1
    random.uniform(minv, maxv) Returns a random float between minv and maxv
    random.choice(str) Returns a random character from the string
    random.shuffle(str) Randomly reorders the collection of characters

Random Examples

    import random from english import * def random_five_letter_word(): random.shuffle(ENGLISH_WORDS) for word in ENGLISH_WORDS: if len(word) == 5: return word def random_no_repeated(): random.shuffle(ENGLISH_WORDS) for word in ENGLISH_WORDS: if not contains_repeated_letters(word): return word

The Portable Graphics Library

  • In this class we use 'pgl.py' which is used in Wordle
  • Just want to introduce it for now
  • Can do: import pgl
  • Operates on the idea of a collage or cork-board
image/svg+xml
  • Newer objects can obscure older objects.

The Pieces

  • At its simplest then, we have two main parts:
    • The window (or felt-board/cork-board)
      • Created with the function gw = GWindow(400,400) # similar to: gw = WordleGWindow()
      • Takes two arguments: a width and a height in pixels
    • The contents
      • A wide assortment of shapes that can be added to the scene
      • Control over where they are placed, how large they are, what color they are, etc

Blue Rectangle!

from pgl import *
gw = GWindow(500, 200) # like WordleGWindow
rect = GRect(150,50,0,0) #  answer = "blimp"
rect.set_color("Blue") # answer.upper()
rect.set_filled(True) 
gw.add(rect) # like word_to_row(answer)

The Coordinate System

image/svg+xml blue_rectangle (0,0) (150,50) 200 px 100 px
  • Positions and distances on the screen are measured in terms of pixels
    • Origin is in the upper left instead of lower left
    • Vertical values increase as you move downwards

Other Simple Objects

Functions to create simple geometric objects:

  • Rectangles/Squares + Circles/Ovals!
    • GRect( x, y, width, height )
    • GOval( x, y, width, height )
    • Creates a rectangle whose upper left corner is at (x,y) of the specified size
  • Lines!
    • GLine( x1, y1, x2, y2 )
    • Creates a line extending from (x1,y1) to (x2,y2)

For Wordle

  • Lower numbered rows are above higher numbered rows.
  • Lower numbered columns are leftward of higher numbered columns.

    (0,0)(0,1)(0,2)(0,3)(0,4)
    (1,0)(1,1)(1,2)(1,3)(1,4)
    (2,0)(2,1)(2,2)(2,3)(2,4)
    (3,0)(3,1)(3,2)(3,3)(3,4)
    (4,0)(4,1)(4,2)(4,3)(4,4)
    (5,0)(5,1)(5,2)(5,3)(5,4)
  • gw.set_square_letter(1, 3, '$') # example
    $

Redact

  • Suppose you want to take two strings, and one is secret (sh!)
  • Examine them to see if they have the same letters at the same position, and
  • "Redact" the letters that are the same in each word. def redact(word:str, secret:str) -> str: for i in range(len(word)): if word[i] == secret[i]: word = word[:i] + '*' + word[i+1:] DEBUG and print('Redact '+secret[i]+' at '+str(i)) return word

Announcements

  • By next Monday 11:59 PM: "Project 1: Wordle"
    • By Tue: Display a word
    • By Wed: Check a guess
    • By Thr: Color letters
    • By Fri: Random word
    • By Sat: Change rows
    • By Sun: Color keys.