Class 6 – Monday, September 9
Fearlessly we march on — variables and strings
A sign for knowing — what the future asks of us — demands we act now
I say love is love — For are'nt we all just people — Living as we're made
Look both ways
Agenda
- Help you better model how a computer works
- Consider assignment subtleties
- Advance your chrestomathics abilities
Illumination
- Extracts from complimenter.py submissions
Examples
- Program variable_boxes.py – shows what Python keeps track of with variables
- Program the_old_switcheroo.py – considers the subltety of swapping variable values
- Program stringing_along.py
- Provides a deeper introduction to the string type
str
Enquiring minds want to know
Program all_consuming.py — due Wednesday September 11
- Develop a program that estimates your annual consumption of a commodity based on how much you consume each weekday and each weekend day.
-
Assumptions
- A week has 5 weekdays and 2 weekend days.
- A year has 52 weeks (yes, we know a real year is longer, but we will ignore that in this program).
- User supplied numbers are integers.
-
Requirements
- The program first prompts the user for the commodity consumed.
- The program then individually prompts the user for two integer inputs: first, the quantity consumed on a typical weekday, then the quantity consumed on a typical weekend day. These are to be requested using two separate prompts.
- The program uses the inputs to compute and report the quantity consumed in a year.
- The second two prompts and the final report are to include the commodity as listed by the user in response to the first prompt. I suggest defining two prompt variables that both make use of the supplied commondity (see notes below)
-
Notes
- You cannot use the comma operator (
,
) for concatenation (string building). If you want to build a string up, use the plus operator (+
).
- After getting the commodity it might prove helpful to then set string variables (e.g.,
weekday_prompt
andweekend_prompt
orprompt1
andprompt2
or ... ) as the prompt strings for getting numeric inputs. And then use those string variables as arguments to theinput()
invocations.
-
Suggested agorithm
- Get input commodity of interest
- Determine weekday consumption prompt
- Determine weekend day consumption prompt
- Separately get weekday and weekend day consumptions
- Compute weekly consumption
- Compute yearly consumption
- Report yearly consumption
Three program runs
What is something you consume daily? paper napkins
Enter paper napkins consumed on a normal weekday: 6
Enter paper napkins consumed on a normal weekend day: 0
You consume 1560 paper napkins per year.
What is something you consume daily? pretzels
Enter pretzels consumed on a normal weekday: 0
Enter pretzels consumed on a normal weekend day: 1
You consume 104 pretzels per year.
What is something you consume daily? jelly beans
Enter jelly beans consumed on a normal weekday: 4
Enter jelly beans consumed on a normal weekend day: 12
You consume 2288 slices of jelly beans per year.
To do list
- Review class artifacts
- Ensure familarity by reading about Python variables, assignment, and input
- Read about Python string capabilities.
- Check out the epistles on Python and problem solving
All you need is love — All you need is love — All you need is love, love — Love is all you need. The Beatles
© 2019 Jim Cohoon | Resources from previous semesters are available. |