Class 6 – Monday, January 28
Fearlessly we march on — strings and input
A sign for knowing — what the future asks of us — demands we act now
Wah-wah — You've given me a wah-wah — And I'm thinking of you — And all the things that we used to do — Wah-wah, wah-wah
— George Harrison
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 math_illiteracy.py
- Demonstrates some useful math and math-like capabilities
- Two different possible runs
Python approximation of pi: 3.141592653589793
Python approximation of Euler's number: 2.718281828459045
sin( 30 degrees ): 0.49999999999999994
tan( 45 degrees ): 0.9999999999999999
Enter two numbers: 12 -15
n1 = 12
n2 = -15
max( n1, n2 ): 12
min( n1, n2 ): -15
abs( n1 ): 12
abs( n2 ): 15
Python approximation of pi: 3.141592653589793
Python approximation of Euler's number: 2.718281828459045
sin( 30 degrees ): 0.49999999999999994
tan( 45 degrees ): 0.9999999999999999
Enter two numbers: -11 19
n1 = -11
n2 = 19
max( n1, n2 ): 19
min( n1, n2 ): -11
abs( n1 ): 11
abs( n2 ): 19
- Program the_old_switcheroo.py – considers the subltety of swapping variable values
- Program stringing_along.py
- Provides a deeeper introduction to the string type
str
Enquiring minds want to know
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 testing of your programs will use inputs different from those shown in problem discussion.
In-class homework requirements
- 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).
Program all_consuming.py — due Wednesday January 30
- 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
- Ensure familarity by reading about Python variables, assignment, and input
- Read about Python string capabilities.
- Check out the epistles on Python and problem solving