Class 6 – Monday, January 29
Fearlessly we march on — strings and input
A sign for knowing — what the future asks of us — demands we act now
Look both ways
Examples
- the_old_switcheroo.py – considers the subltety of swapping variable values
- stringing.py – provides a deeeper introduction to the string type
str
- all_consuming.py – chrestomathics
Agenda
- Help you better model how a computer works
- Consider assignment subtleties
- Advance your chrestomathics abilities
General requirements (again)
- 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 are to be named indicating their purpose/usage.
- Always end each prompt with a space; e.g.,
input( 'What is something you consume daily?: ' )
- Demonstrate to a classmate that you have met the problem specifications. Identify those person(s) as part of the header comments for your programs.
- Ensure your checkers have also been successfully checked out by somebody(s).
Enquiring minds want to know
Program all_consuming.py
Program all_consuming.py
- 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
-
Two program runs
What is something you consume daily? slices of cheese
Enter slices of cheese consumed on a normal weekday: 4
Enter slices of cheese consumed on a normal weekend day: 12
You consume 2288 slices of cheese per year.
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.
To do list
- Review class artifacts
- Read about Python string capabilities.
Warning: fopen(/l/cs1112/public_html/defs/htm/181-trailer): Failed to open stream: No such file or directory in /l/cs1112/public_html/defs/php/read.php on line 5
Unable to open file!