Class 19 — Wednesday, October 6

Decisions made easy

Put cell phones away — Please do no multitasking — Or under-tasking


Look both ways


Agenda


To do list


Examples (redownload)


Yesterclass

Enter soil pH level: 6.5

pink

Enter soil pH level: 7

blue

Enter soil pH level: 7.5

blue



The why, what, and how of an if statement — revisited and expanded.

if ( logical expression ) :

    action1

else :

    action2

  • Where logical expression is a test expression that evaluates to either True or False.
  • Both action1 and action2 are non-empty sequence of statements.
  • The actions are indented one-level further than the start of the if statement.
  • The else is indented at the same level as the if. Both if and else are keywords:

if ( logical expression ) :

    action

  • If the logical expression evaluates to True, the action is executed. Otherwise, no action occurs.

if ( logical expression1 ) :

    action1

elif ( logical expression2 ) :

    action2

...

elif ( logical expressionk ) :

    actionk

else : # optional

    actionk+1


Problem begins_with_a_vowel.py

Enter text: apple

Text begins with vowel

Enter text: banana

Text does not begin with vowel

Enter text: Orange

Text begins with vowel



Problem daisy_daisy.py

Enter day of week: Sunday

Weekend day

Enter day of week: monday

Start of school week

Enter day of week: TUESDAY

School day

Enter day of week: Wednesday

School day

Enter day of week: Thursday

School day

Enter day of week: Friday

End of school week

Enter day of week: Saturday

Weekend day



Problem robotomy.py

Door (locked / unlocked): locked

Turn around

Door (locked / unlocked): Unlocked

Open the door

Light (on / off): on

Enter the room

Door (locked / unlocked): unlocked

Open the door

Light (on / off): Off

Turn on the light

Enter the room



Problem spell_check.py

Enter text: It was the beste of times It was the wurst of times It was Oktoberfest

beste

wurst

oktoberfest

Enter text: how much wod can a woodchuck chuck

wod

woodchuck

Enter text: to be or not to be that is the question

Enter text:

  • Import our url module.
  • Specify the URL for the spelling dictionary.
  • Read the contents of the URL and store it as a list of word spellings.
  • Get the user text and convert it into a list of words.
  • For each user word, determine whether it is correctly spelled. If it is incorrectly spelled, print it out.

Decision making

deciding which shoes to buy



Where in the world am I

jpc at stonehenge