Class 05 – Friday, January 28
It's all about you
Show understanding — Put it altogether now — Yes you all can do
Look both ways
Downloads — get them all, some have been updated
- Program floatilla.py
- Program favorite_word.py
- Program combine_replicate.py
- Program hip_hopping.py
- Program complimenter.py
- Program dateable.py
Program floatilla.py
-
Problem
- Introduce the
float
type and decimal arithmetic
-
Program run
Values of interest
x: 2.71
y: 3.14
Results
x + y: 5.85
x - y: -0.43000000000000016
x * y: 8.5094
x / y: 0.8630573248407643
x // y: 0.0
x ** y: 22.883559193263366
x % y: 2.71
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. That meansprint(3+2)
is not okay, style-wise.
- 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?: ' )
-- (otherwise it looks ugly-- the user's input starts typing right next to the last word!)
- The grading of your programs will use inputs different from those shown in problem discussion.
Program favorite_word.py
input()
and len()
-
Problem
- Prompt and get a word of interest from user
- Compute the length of the word
- Output a message about the length of the word
-
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.
- Introduces the built-in function
len()
.
-
Three possible program runs
Tell me a favorite word: zeitgeist
Did you know that zeitgeist has length 9
Tell me a favorite word: akimbo
Did you know that akimbo has length 6
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.
🦆 © 2022 Jim Cohoon | Resources from previous semesters are available. |