Class 05 – Friday, September 1
It's all about you
Show understanding — Put it altogether now — Yes you all can do
Help crush breast cancer — For your families and friends — Not be asunder
Look both ways
Downloads — get them all, some have been updated
- Program cast_away.py
- Program favorite_word.py
- Program combine_replicate.py
- Program hip_hopping.py
- Program complimenter.py
- Program dateable.py
Agenda
- Further expand our communication skills
- Introduce Python string operators
+
and*
- Get adept at casting
- Demonstrate your own chresthomatics abilities by solving and submitting problem solutions before the end of class.
Future homework requirements and facts
- A numeric value display must be from the value of a variable; i.e., there will never be arithmetic calculations within a
print()
statement.
- All variables should be named indicating their purpose/usage.
- Always end each prompt with a space; e.g.,
input( 'What is something you consume daily?: ' )
- The grading of your programs will use inputs different from those shown in problem discussion.
Program cast_away.py
-
Problem
- Demonstrate how to convert values of one type to values of other types
-
Program run
i: 1
f: 2.9
c: '33'
d: '4.5'
int( f ): 2
float( i ): 1.0
int( c ): 33
float( d ): 4.5
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: crwth
Did you know that crwth has length 5
Tell me a favorite word: strengths
Did you know that strengths has length 9
Tell me a favorite word: scraunched
Did you know that squirreled has length 10
Program hip_hopping.py
-
Problem
- Simulate the number of rabbits occurring over four generations.
- User supplies two value: the number of rabbits in the initial population and the growth rate (the number of new rabbits each rabbit contributes to the next generation)
-
What the solution offers
- Converts input into integer values
- Examines nuances of the assignment operator.
-
Two possible program runs
How many rabbits: 2
Growth rate per generation: 2
Generation: 1
Rabbits: 2
Generation: 2
Rabbits: 4
Generation: 3
Rabbits: 8
Generation: 4
Rabbits: 16
How many rabbits: 6
Growth rate per generation: 2
Generation: 1
Rabbits: 6
Generation: 2
Rabbits: 12
Generation: 3
Rabbits: 24
Generation: 4
Rabbits: 48
How many rabbits: 4
Growth rate per generation: 5
Generation: 1
Rabbits: 4
Generation: 2
Rabbits: 20
Generation: 3
Rabbits: 100
Generation: 4
Rabbits: 500
Program combine_replicate.py
- Introduces the string concatenation operator
+
and replication operator*
.
-
Some possible progrm runs
Enter word: fire
Enter word: fighter
Enter phrase: Wah-Hoo-Wah!
word1: fire
word2: fighter
phrase: Wah-Hoo-Wah!
### operator + performs concatenation
word1 + word2: firefighter
### operator * performs concatenation
n: 3
n * phrase: Wah-Hoo-Wah!Wah-Hoo-Wah!Wah-Hoo-Wah!
Program complimenter.py
-
Problem
- For a single user-supplied integer age, print out a tasteful compliment based on the supplied age.
-
Requirements
- The faux (complimentary) age is the user-supplied age minus five years.
- The faux age is to be integer.
- The program does not attempt to get more than one input from the user.
-
Three different possible program runs
- The above section header means what it literally says. The below boxes each show a different program run; i.e., your program is to perform a single age calculation each time it is run.
Enter your age: 19
You don’t even look 14
Enter your age: 111
You don’t even look 106
Enter your age: 20
You don’t even look 15
-
Suggested code outline
- Prompt user to supply an age
- Convert user reply to integer
- Compute supposed age
- Print the result of the computation
Program dateable.py
-
Problem description
- For a single user-supplied age, tell them how old a person must be to have dating be acceptable according to the following folk rule:
- You should only date someone who is at least seven years older than than half your age.
- For example, an 18 year old needs to date somebody at least 16 year's old (i.e., 7 + ( 18 / 2 ) ).
-
Requirements
- The dateable age is to be calculated by using the above folk rule.
- Use separate variables for storing the user-supplied and dateable ages.
- The displayed dateable age is to be integer.
-
Three different possible program runs
Enter your age (integer): 19
19 year olds should date somebody who is at least 16 years old
Enter your age (integer): 22
22 year olds should date somebody who is at least 18 years old
Enter your age (integer): 88
88 year olds should date somebody who is at least 51 years old
-
Suggested algorithm
- Prompt user to supply an integer age
- Convert user reply to integer
- Compute minimum age according to dateability formula
- Print the result of the computation
To do list
- Review class artifacts
- Ensure familarity with Python variables, assignment, and input
- Definitely check out the string module. It will be a major topic next week
- Reread the epistle on
variety.py
- Complete current assignments as soon as possible for your own sake.