Introduction to Python
davidbpython.com
Project Discussion, Session 9
9.1 | Notes typing assignment. Please write out this week's transcription notes. |
|
|
PROJECTS 9.2 - 9.5: CONVERTING PROGRAMS TO FUNCTIONS
|
|
To write the function:
|
|
9.2 | Function get_input_as_int. All you need to do is write a function that takes one argument, converts that value to an int and returns the int. If the argument is not an int value (i.e., not all digits) then the code should raise a ValueError. |
Please make special note of the argument and return value specified in the docstring. Make sure your function expects one argument and returns one return value. Make sure not to print the result inside the function. When you are are ready to test, you should be able to run the program and see the results indicated. |
|
9.3 | Function file_count. This is based on the word count program that was an extra credit in Session 4. It opens the file and counts the number of lines, words and/or characters in the file. |
The overall purpose of this assignment is to get you to think of a file as a whole string, a list of strings, and as a list of words. To determine the number of characters in the file, you need only think of the file as a string, and get the string length. To determine the number of lines in the file, you need only think of the file as a list of lines, and get the list length. To determine the number of words in the file, you need only think of the file as a list of words, and get the list length. How can we think of the file as a string? file.read() returns the file contents as a string. We can use len() on a string to see how many characters are in the string. How can we think of the file as a list of lines? str.splitlines() returns the string split on the newline character, i.e. a list of lines. We can use len() on a list to see how many elements are in the list. How can we think of the file as a list of words? str.split() returns a string split on whitespace, i.e. a list of words. We can use len() on the list to see how many words are in the list. Therefore you should not open the file more than once, because both lines and words can be derived from the single string returned by .read(). Function mechanics: instead of taking user input, simply have the function expect the filename and 'items=' as arguments. So your program WILL NOT USE input(). Of course the optional argument items= must have a default value. I suggest making the default None and using the test if not items to see if there is no value. |
|
To see if items is not any of 'c', 'w', 'l' or None, use this idiom:
if items not in ['c', 'w', 'l', None]:
# raise ValueError here
|
|
Please make special note of the argument and return values specified in the docstring. Make sure your function expects one argument and returns one or three return values. Use a return statement to return the value(s). Make sure not to print the result inside the function. When you are are ready to test, you should be able to run the program and see the results indicated. If you get an error, make sure to review the possible errors noted in the assignment. |
|
9.4 | Function get_lookup_dict. This function takes a filename, reads the file line by line, and builds a dict with the first column of values as the key and the 2nd column of values as the value in the dict. |
Please make special note of the argument and return value specified in the docstring. Make sure your function expects one argument and returns one return value. Make sure not to print the result inside the function. So the function starts with the filename argument. You can use some of the code you wrote in the "lookup dict" assignment to build the dict. Loop through the file line by line, split each line into two items, and set the first item as the key and the 2nd item as the value inside the dict. When you are are ready to test, you should be able to run the program and see the results indicated. Make sure not to print the result inside the function. If the second call shows you the items from both pacman_ghosts.csv and the new file, consider how the code is executing, at what point the dict is being updated, and run through the execution of the code in your mind. Obviously when the second call is made, the dict already has the pacman data in it -- why? |
|
9.5 | Build a module of your functions. |
|
|
9.6 | Write a sentence or two describing how you'd like to use Python. Go wild! |