Class 05 – Wednesday, February 10
It's all about you
Show understanding — Put it altogether now — Yes you all can do
Look both ways
Downloads
- Program casting.py
- Program being_powerful.py
- Program trio.py
- Program complimenter.py
- Program tao.py
Agenda
- Further expand our communication skills
- Enlightenment
- Introduce objects
- Introduce Python string manipulation capabilities
- Get adept at casting
- Demonstrate your own chresthomatics abilities by solving and submitting problem solutions before the end of class.
All future homework requirements
- 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.
To do list
- Check out the the activities for next class
- Ensure familiarity of the past by carefully examining artifacts, epistles, and videos.
- Complete homework 06 as soon as possible for your sake.
- Definitely check out the string module. It will be a major topic next week
Program casting.py
-
Introduce type conversion — constructing a value of one type from a value of another type
-
Major conversions
int( s )
: returns the integer constructed from integer numeric strings
int( d )
: returns the integer part of decimald
float( s )
: returns the decimal constructed from numeric strings
. There maybe loss of precision
float( i )
: returns the decimal constructed using integeri
. There maybe loss of precision
str( x )
: returns a string version ofx
- A possible program run
integer_string = '112358'
decimal_string = '93.86'
decimal_number = 9.04
integer_number = 1112
i1 = int( integer_string )
i2 = int( decimal_number )
f1 = float( integer_number )
f2 = float( decimal_string )
i1 + i2 = 112367
f1 + f2 = 1205.86
nbr1 = 14
nbr2 = 1.5
s1 = str( nbr1 )
s2 = str( nbr2 )
s1 + s2 = '141.5'
Program being_powerful.py
-
Problem
- Prompt and get an integer number (the base for the computation)
- Prompt and get a decimal number (the exponent for the computation)
- Compute the value of the integer to power of the decimal number
- Print the result of the computation
-
What the solution offers
- Uses the built-in cast functions
int()
andfloat()
to convert numeric strings into the numbers they represent.
-
Three possible program runs
Enter base (integer): 2
Enter exponent (decimal): 0.5
1.4142135623730951
Enter base (integer): 11
Enter exponent (decimal): 3.14
1861.971509290233
Enter base (integer): 9
Enter exponent (decimal): 99.9
2.132201+95
Program trio.py
-
Problem
- Prompt user for three words
- Split the input into separate words
- Print the words on separate lines
-
What the solution offers
- Introduces the string member function
split()
. Its called a member because every string can be called upon to produce a split up version of itself. The value of asplit()
invocation is the list of substrings (words) that make up the string being told tosplit()
. The function is handy for breaking up user input into its individual parts.
- The final version of the program shows that every string has a member function for inspecting itself. The
split()
function hands back the list of words that make up the string.
-
Three possible program runs
What are your three favorite words: peace joy friends
peace
joy
friends
What are your three favorite words: you me us
you
me
us
What are your three favorite words: subcontinental nudiustertian uncopyrightable
subcontinental
nudiustertian
uncopyrightable
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 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 tao.py
-
Problem
- Prompt and input from user both an integer width and height of a rectangle
- Split the input into two strings
- Convert strings into integers
- Compute the product
- Produce the result
-
What the solution offers
- Shows the proper way of converting the splitting of a string made up of numberic characters into individual numbers.
-
Three possible program runs
Enter rectangle width and height (integers): 5 6
30
Enter rectangle width and height (integers): 2 71
142
Enter rectangle width and height (integers): 3 14
42
🦆 © 2022 Jim Cohoon | Resources from previous semesters are available. |