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:

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

  1. 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()
  2. 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
  3. Write a line of code that uses split to split the user's input into a list, and saves that list into a variable.
  4. 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.
  5. 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.
  6. 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: 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
    
  7. 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
    
  8. 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
  9. 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.
    
  10. 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.
    
  11. 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.
  12. Continue to Part C.