Class 07 – Monday, February 15
Make Bill Murray proud
Take another step — Chrestomathics awaits you — Feel the momentum
Camouflage not needed — Do not hide what makes you, you — You are welcomed here
Look both ways
Important survey
- Please take it.
What I want to accomplish today
- Complete our introduction of the basics of Python programming by examining a program that converts Celsius temperatures to Fahrenheit temperature. The program is important as makes us explicitly aware of operator precedence.
- To solve problems of greater complexity we need to add to our programming toolbox. In particular, we need to be able to decompose complex inputs into manipulable data and then be able to process the data no matter it size.
- Such abilities requires deeper awareness of string manipulation and the ability to iterate; that is, be able to repeat actions so that all the data can be processed. The slang word for iteration is looping.
I should not show this complimenter.py interaction, but I am going to do it anyway
How old are you? 24
If aliens descended from space, they would classify you as 19
Agenda
- Continue exploration of the basics of problem solving — getting data, performing computation, and displaying information. Along the way we will develop an appreciation for expression evaluation and formulation
- Continues introduction to the string type
str
Downloads
- Program temperature.py
- Considers operator precedence
- Program break_the_bank.py
- Continues chrestomathics
- Program indexing.py
- Weird world of indexing in Python
- Program lickety_strip_and_split.py
- Explores the string member functions
strip()
andsplit()
.
- Program love_is_all_you_need.py
- Introduces for loops that process a range of values.
- Program thats_amore.py
- Introduces for loops that process a list of data.
To do list
- Review class artifacts
- Finish homework
- Check out other Python built-in functions
- Reread about Python string capabilities.
- Reread problem-solving epistle
Program temperature.py
- Prompts user for a integer Celsius temperature and separately computes its integer and decimal Fahrenheit equivalent using the formula
- Two different possible runs
Enter Celsius temperature: 24
24 C = 75.2 F
24 C = 75 F
Enter Celsius temperature: 8
8 C = 46.4 F
8 C = 46 F
Program break_the_bank.py
- With a single prompt, it gets from the user four values in the following order: number of quarters, dimes, nickels, and pennies. Computes and displays how much the coinage is worth.
- To avoid runs like the following, you are required to make me use of built-in function round() to convert the calculated worth of the piggy bank to two decimal places.
- Two possible testing runs
Number of quarters, dimes, nickels, and pennies: 27 14 28 18
Coinage
27 quarters
14 dimes
28 nickels
18 pennies
are worth 9.73 dollars
Number of quarters, dimes, nickels, and pennies: 1 2 3 4
Coinage
1 quarters
2 dimes
3 nickels
4 pennies
are worth 0.64 dollars
Program indexing.py
- Explores selecting an individual character from a string — indexing
- Introduces indexes.
Program output
### [] is the string index operator
s: sequoia
0123456
n = len( s ): 7
s[ 0 ]: s
s[ 4 ]: o
s[ n-1 ]: a
Program lickety_strip_and_split.py
- Explores the string member functions
strip()
andsplit()
.
q: ' There is a door open to walk through '
### mis-attempted stripping
q.strip()
q: ' There is a door open to walk through '
### stripping
s = q.strip()
q: ' There is a door open to walk through '
s: 'There is a door open to walk through'
### splitting
f: 'bananas $0.69'
c: 'SEAS CS 1112'
fruit, price = f.split()
bananas costs $0.69 per pound
school, subject, number = c.split()
School: SEAS subject: CS number: 1112
### list making
list1 = f.split()
list2 = c.split()
list1: ['bananas', '$0.69']
list2: ['SEAS', 'CS', '1112']
Program love_is_all_you_need.py
- Prints the adage Love is all you need the requested number of times
-
Some possible program runs
Enter number of lines to be printed: 5
Love is all you need
Love is all you need
Love is all you need
Love is all you need
Love is all you need
Enter number of lines to be printed: 1
Love is all you need
Enter number of lines to be printed: 0
Program thats_amore.py
- Prompt user for text and print the words one per line
-
Some possible program runs
Enter text: love amore salanghae
love
amore
salangha
Enter text: upendo grá cinta Ai
upendo
grá
cinta
Ai
🦆 © 2022 Jim Cohoon | Resources from previous semesters are available. |