CS 115 Lab 7, Part B: Find the average word length in multi-line text
[Back to lab instructions]
Specification
Before you start programming, read this specification to understand
what your final code should do. Step-by-step instructions will follow.
Your final program should:
- Repeatedly ask the user to enter a line of text.
Stop asking when the user
enters a blank line (that is, hits ENTER without typing any text).
- Compute and print the average length of all the words the user entered.
Our definition of a "word" is a chunk of text set off by whitespace.
For example, @#$! is a 4-character "word" by this definition.
- Your program should be able to handle the case where the user types
only a blank line.
Sample output #1:
Enter some text: This little piggie went to market
Enter some text: This little piggie stayed home
Enter some text:
The average word length is: 4.90909
Sample output #2:
Enter some text:
You did not enter any words.
Instructions
- Create and open a new Python source code file called lab07b.py:
"""
Program: CS 115 Lab 7b
Author: Your name
Description: Computes the average word length of the user's text.
"""
def main():
main()
- Write a line of code that asks the user for a line of text and saves it to a variable. Then write a separate line to print the user's input:
Enter some text: Mary had a little lamb
You entered: Mary had a little lamb
- Write a line of code that uses split to split the user's input into a list,
and saves that list into a variable.
- Replace your current print statement with a statement that prints the number of words
in the line you just read.
Enter some text: Mary had a little lamb
This line has 5 words.
- Add some code that loops over all of the words in your list and computes the
total number of characters in all of the words. Print this total. For example:
Enter some text: Mary had a little lamb
This line has 5 words.
These words have a total of 18 characters.
- Put the code you have written so far inside a loop that repeatedly asks the user for
a line of text until the user enters a blank line. You have two options for
recognizing a blank input string:
- Option 1: A blank input string will have a length of 0.
- Option 2: A blank (empty) input string is represented by two quotation
marks with nothing in between them.
Also modify your code so that it uses a new variable to add up the number of words on all of the lines and prints that value after all of the input has been entered. Don't worry about the number of characters for now.
Enter some text: Mary had a little lamb
These words have a total of 18 characters.
Enter some text: Jack and Jill went up a hill
These words have a total of 22 characters.
Enter some text:
Total: 12 words
- If it's not already doing so, modify your code to add the number of characters
on all of the lines instead of just one line at a time:
Enter some text: Mary had a little lamb
Enter some text: Jack and Jill went up a hill
Enter some text:
Total: 12 words
Total: 40 characters
- Instead of printing the total length and the number of words
entered, print the average word length for the user's text, and round it
to 5 decimal places:
Enter some text: Mary had a little lamb
Enter some text: Jack and Jill went up a hill
Enter some text:
The average word length is: 3.33333
Here is another example:
Enter some text: This little piggie went to market
Enter some text: This little piggie stayed home
Enter some text:
The average word length is: 4.90909
- Run your program and immediately hit ENTER when the user asks you
for some text. Your program will probably crash. Answer Question 20
in your writeup, and then write some code to fix this problem if your program did crash. Sample input/output:
Enter some text:
You did not enter any words.
- Make sure your code matches the examples from the specification:
Enter some text: This little piggie went to market
Enter some text: This little piggie stayed home
Enter some text:
The average word length is: 4.90909
and
Enter some text:
You did not enter any words.
- When your code matches the sample outputs, call an instructor over to demo. This is also the code you will submit on Moodle this week.
- Continue to Part C.