Class 4 – Monday, February 8
Variation on a theme
A variable — Is like a box or a name — We must all know this
The #metoo movement — Calls out to everyone — Female Lives Matter
Look both ways
What is happening today
- Want you to have a mental model of how to step through code.
- Become reactive
Download as soon as you can
A code walk through — getting a mental model of variables and assignment
Agenda
- Further expand our communication skills
- Consider abstraction
- Expand our understanding of variables
- Introduce assignment
- Introduce input acquisition
Important questions
- What is a variable?
- Why is assignment so important
- Does the old value of a variable hang around for us to use?
Program variety.py
-
Problem
- Perform several assignment statements including an update of a variable.
-
What the solution offers
- Basic understanding of variables and assignment.
- The ability to update is crucial when problem solving.
-
Check out after class
- Different walkthrough
Program hip_hopping_down_the_bunny_trail.py
-
Problem
- Simulate the number of rabbits occurring over five generations.
-
What the solution offers
- Examines some nuances of the assignment operator.
-
Expected output
Generation: 1
Rabbits: 2
Generation: 2
Rabbits: 4
Generation: 3
Rabbits: 8
Generation: 4
Rabbits: 16
Generation: 5
Rabbits: 32
-
Note for later
- The artifact version of the program updates the values of the variables in terms of themselves.
Program echo.py
-
Problem
- Prompt, get, and echo text from the user
- Prompt, get, and echo more text from the user
-
What the solution offers
- Introduces built-in function
input()
. When the function is invoked, its argument is printed to the display, the entry supplied by the user in response to that message is the value of the invocation. Theinput()
function always returns the user reply as a string.
- The
input()
function allows programs to be interactive; i.e., their actions can be influenced by the user. Theinput()
function enables Python to do general problem solving.
-
Three possible program runs
Please provide text: My favorite color is sometimes electric blue
Your reply was: My favorite color is sometimes electric blue
Please provide more text: I know someone whose favorite number is 88
Your new reply was: I know someone whose favorite number is 88
Please provide text: The name Hannah is a palindrome
Your reply was: The name Hannah is a palindrome
Please provide more text: I sometimes misspell misspell as "mispell"
Your new reply was: I sometimes misspell misspell as "mispell"
Please provide text: I like the word crwth
Your reply was: I like the word crwth
Please provide more text: Do you know why I like the word crwth
Your new reply was: Do you know why I like the word crwth
Program favorite_word.py
-
Problem
- Prompt and get a word of interest from user user
- Compute the length of the word
- Output a message about the length of the word
-
What the solution offers
Another use of the built-in function
input()
.
Introduces the built-in function
len()
.
-
Three possible program runs
Tell me a favorite word: rhythms
Did you know that rhythms has length 7
Tell me a favorite word: strengths
Did you know that strengths has length 9
Tell me a favorite word: squirreled
Did you know that squirreled has length 10
Program combine_replicate.py
- Introduces the string concatenation operator
+
and replication operator*
.
### operator + performs concatenation
a = fire
b = fighter
a + b = firefighter
### * operator produces repeated concatenation
m = Wahoo-Wah!
n = 3
m * n = Wahoo-Wah!Wahoo-Wah!Wahoo-Wah!
n * m = Wahoo-Wah!Wahoo-Wah!Wahoo-Wah!
Program psychoanalyst.py
-
Problem
- Prompt and get what is on the user’s mind
- Prompt and get why the first reply is on their mind.
- Print an out of time message
-
What the solution offers
- Introduces the
str
type operator+
. The+
operator when evaluated with two strings as its operands, produces as its value, their concatenation.
- Another use of the built-in function
input()
.
- Creates and uses an input prompt that makes use of a prior input.
-
Three possible program runs
Tell me what is on your mind: I worry about the future
Hmmm. Why is that on your mind: Because it it coming
Oh? I wish we had more time to chat. So long.
Tell me what is on your mind: A fluttering butterfly
Hmmm. Why is that on your mind: I do not want rain
Oh? I wish we had more time to chat. So long.
Tell me what is on your mind: Is superman colorblind
Hmmm. Why is that on your mind: To account for fashion sense
Oh? I wish we had more time to chat. So long.
-
Three prefered possible program runs
Tell me what is on your mind: I worry about the future
Hmmm. Why is -- I worry about the future -- on your mind: Because it it coming
Oh? I wish we had more time to chat. So long.
Tell me what is on your mind: A fluttering butterfly
Hmmm. Why is -- A fluttering butterfly -- on your mind: I do not want rain
Oh? I wish we had more time to chat. So long.
Tell me what is on your mind: Is superman colorblind
Hmmm. Why is -- Is superman colorblind -- on your mind: To account for fashion sense
Oh? I wish we had more time to chat. So long.
To do list
- Review class artifacts
- Ensure familarity with Python variables, assignment, and input
- Look over the Python string module (library)
- Read the epistle on
variety.py
- Re-examine the walkthrough
- Look over another walkthrough
- Do current assignment
- Read, but do not do, the next assignment
🦆 © 2022 Jim Cohoon | Resources from previous semesters are available. |