M04 Assignments


The lessons for M04 concentrate on lists, so every program should include a list to hold temporary data.

M04 Exercise #1 (Collect Sales Data By Day)

This is very similar to the sample program included in Chapter 7.

But note:

  • Unlike the sample program, do NOT include code in main() except for displaying a banner and calling other functions
  • Instead of Day #1, Day #2, display the name or abbreviation for each day of the week as part of the prompt for the daily sales (hint: make a list of days)
  • Separate the code for collecting the data from the code for displaying the results into more than one function
  • As the user enters data for each day, store it in a list for that purpose
  • Redisplay the day of the week when displaying the list of entries
  • Aggregate and display the total sales for the week
  • Also display the average daily sales for the week

Output example:

M04 Exercise #1 Sample Output

Figure 1: M04 Exercise #1 Sample Output

M04 Exercise #2 (Look up Popular Names)

Read the files for popular boy and girl names into two lists. Prompt the user to search for a boy's name or girl's name, and a name to search for. This should happen in a WHILE loop, so the user can continually search for different names until he or she wishes to exit. Be careful that you skip prompting for the name if the user indicates they wish to exit instead of choosing a gender. If the user exits, display a thank you message of some type.

Once the user provides a gender and name to look up, iterate through the appropriate list (using a for loop) and try to match the search name with names in the list.

Use the .lower() string function to try matching names only on lowercase versions of the search name and list names. (See chapter 8, page 422 for usage)

If you find the name, tell the user where it was in the list (how popular it was). If you don't find the name, display a message indicating the name was not in the list.

  • You may use a single function, if desired, to search for either a boy name or a girl name
  • You may have to use the global keyword to read the file lines into a global list (see below)
  • Make sure and test using names in each list and not in each list (see the output sample below).

Using global Keyword

The global keyword can be used to explicitedly tell Python that a locally defined variable refers to a global variable. For this assignment, it allows you to define lists at the global level and then populate them inside a function that reads the data files.

M04 Exercise #1 Sample Code

Figure 2: M04 Exercise #1 Sample Code for global keyword

Output example:

M04 Exercise #2 Sample Output

Figure 3: M04 Exercise #2 Sample Output

M04 Exercise #3 (Analyze Text File)

This is loosely based on Chapter 8 Exercise #7.

Read the provided text file and count the characters in the following categories:

  • All
  • Uppercase
  • Lowercase
  • Digits
  • Whitespace
  • Punctuation

Set up the above categories in a list. Create a list to hold the counts.

Read the entire file into a variable. Iterate through the string variable one character at a time.

Chapter 8 discusses the built-in string functions to identity when a character or string is upper, lower, digits or whitespace.

For cases where a character is not any of the above categories, use the IN operator (or find() function) to determine if the character is a punctuation character (make a single string of the four punctuation marks in this file: . , - ': period, comma, dash, apostrophe)

Display the results by simply iterating through the two lists (2 lines of code)

Output example:

M04 Exercise #3 Sample Output

Figure 4: M04 Exercise #3 Sample Output