Class 6 – Monday, September 10
Fearlessly we march on — strings and input
A sign for knowing — what the future asks of us — demands we act now
Look both ways
Form
- Help improve CS 1112
Agenda
- Help you better model how a computer works
- Consider assignment subtleties
- Advance your chrestomathics abilities
Examples
- the_old_switcheroo.py – considers the subltety of swapping variable values
- stringing.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 dateable.py — due Monday September 10
- For a single user-supplied age, tell them how old a person must be to have dating be acceptable according to the following folk rule:
- You should only date someone who is at least seven years older than than half your age.
For example, an 18 year old needs to date somebody at least 16 year's old (i.e., 7 + ( 18 / 2 ) ).
-
Requirements
- The dateable age is to be calculated by using the above folk rule.
- Use separate variables for storing the user-supplied and dateable ages.
- The displayed dateable age is to be integer.
-
Three different program runs
Enter your age (integer): 19
19 year olds should date somebody who is at least 16 years old
Enter your age (integer): 22
22 year olds should date somebody who is at least 18 years old
Enter your age (integer): 88
88 year olds should date somebody who is at least 51 years old
-
Suggested algorithm
- Prompt user to supply an integer age
- Convert user reply to integer
- Compute minimum age according to dateability formula
- Print the result of the computation
Program all_consuming.py — due Wednesday September 12
- 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
© 2019 Jim Cohoon | Resources from previous semesters are available. |