Functions

Functions. They make programmers look good. Instead of having a single main() function with 1,200 lines of code in it, you can now break up your code into nice, reusable pieces of code. Commander Data is fully functional, he knew it was a good idea.

Your Assignment

In this assignment you will revisit reading data from a file, and use that data as arguments (parameters) for a number functions you will write.

You will need to:

  1. Write and test a function square_each(nums)
    • Where nums is a (Python) list of numbers. It modifies the list nums by squaring each entry and replacing its original value. You must modify the parameter, return will not be allowed!
  2. Write and test a function sum_list(nums)
    • Where nums is a (Python) list of numbers. It returns the sum of the numbers in passed-in the list.
  3. Write and test a function to_numbers(str_list)
    • Where str_list is a list of strings, each of which represents a number. Modify each entry in the list by converting it to a number. You can choose to use return the list, or just modify str_list in-place, similar to #1, above.
  4. Write and test a function main()
    • Take the previous three functions and construct a main() function that will call these functions properly. The program computes the sum of the squares of numbers read from a file. Your program should prompt for a file name and print out the sum of the squares of the values in the file. Hint: use readlines()

You may download two example files of numbers to read in, numbers (space seperated) or numbers (comma seperated). You may choose to read in numbers in another format of your choosing, so long as you choose one method and document it in your program comments.

Output

This example parses the numbers_csv.txt file. The result should be the same regardless of which input data was used.

This program computes the sum of the squares of numbers read from a file.
Please enter the file name: numbers_csv.txt
The sum of the squares of the numbers in the file is 32932.0

Submission Requirements

Your file is required to be named lab_functions.py.

Please read and follow the Submission Requirements to avoid point deductions specified below.

Grading

The following is the grading rubric for this lab to be used by the course graders. The lab is worth 100 points.

Note: There may be additional criteria for grading, this is just a summary of the major rubric items.

Grading Rubric
Type Description Point Loss (Max)
  [General]  
SyntaxError Exceptions A program that doesn’t even start due to syntax errors. -30
Docstrings Missing proper lab header (e.g. no name, lab section, etc) -10
main() Your program should have main() as the first-called function -10
PEP 8 Style Egregious problems with your code styling -10
Incorrect Filename Follow the submission instructions -5
Commenting Insufficient or excessive comments -5
  [Lab Specific]  
Logical Errors Logical errors in the functions -5
Functions The program is required to have four functions (main(), square_each(nums), sum_list(nums), to_numbers(strlist)) -15/each missing
square_each(nums) The function square_each(nums) is required modify its parameter, the list nums -7
sum_list(nums) The function sum_list(nums) takes as a parameter the list nums, and computes a single sum value of all the numbers in this list -7
to_numbers(strlist) The function to_numbers(strlist) can modify its parameter, the list nums or construct a new list and return it -7
main()
The function main() needs to properly call your three functions,
in the right order, and also print out the sum of the squares if the numbers you read in
-7

Extra Credit

Extra credit work must be submitted in what would be your original submission file for the assignment. All extra credit code must be clearly commented in order to receive any points.

  • +5 points

    Make your program flexible enough to read in data that is separated by either commas, or spaces.