CS 115 Lab 9 If statements and While Loops and Flags (Team)
This lab is due by Monday November 6 .
Submit links: Team Lab 9, Code
Educational goals of this lab - verify that every student can
- understand and write if statements
- understand and write while loops
- use a flag
- watch the videos about
- Documenting a function
- Scope and Lifespan of Identifiers
- While loops
- Sentinel Logic
- Data Validation
- read about Sentinel Logic
- (40 points) Problem #1:
- The program should ask the user for names until they enter N.
- There can be any number of names.
- After the user enters N, the program should report the total length
of all the names that were entered. Here you need to use the function
len(). It takes a string as an argument and returns an integer that
is how many characters are in the string. len("ABC") = 3, len('') = 0
- Note that your program should handle any amount of data, not
just the example run below.
- Note: this program MUST use sentinel logic - with "N" as the sentinel value.
Do NOT start the name variable out as "" or something like that to get into
the loop. It is not necessary to give a dummy value here; use a priming read.
Follow the pattern for sentinel logic described in the reading.
Enter a name or N to stop: john
Enter a name or N to stop: Ralph Smith
Enter a name or N to stop: Mary Slone
Enter a name or N to stop: Guido van Rossum
Enter a name or N to stop: N
The total length of the names entered is 41
- (10 points) Problem #2: Now that you have the program working, change the loop condition slightly so
that it handles upper or lower case N as input. Use a Boolean operator.
- (20 points) Problem #3:
Now that you have that working, add a flag that will tell you if
any input was "Dan". All the flag has to detect is that Dan was entered
as the input at least once or not at all.
Add an if/else after the loop to report the result of the flag.
The loop does NOT stop if Dan is input, the fact is just recorded by the flag.
The name is exactly "Dan", you don't have to worry about Dan being a substring of the input.
- (5 points) What happens if you answer the program's input prompt by just pressing the Enter key?
Put this answer in a comment in your program at the bottom.
- Sample run with modifications:
Enter a name or N to stop: Samuel
Enter a name or N to stop: Jacob
Enter a name or N to stop: Bryan
Enter a name or N to stop: Matt
Enter a name or N to stop: n
The total length of the names entered is 20
There were no Dan's
- (10 points) Problem #4: Test Cases
Write 6 test cases for this program. Three of the cases should be for the loop; three of
them should be for testing the flag. Put them at the bottom of your program in a comment.
- Submit your finished
program with the link at the top of the page.
Use the menu choices of Lab 9 and Code.
Log off properly - you don't want your account misused by someone else!
Remember NOT to leave files
on the local hard drives in this lab or anywhere else on
campus! Make sure you save your projects onto a portable
storage device you take with you!