Lab Assignment
Lab Assignment
Objectives
• To work with strings
• To learn to use the Python documentation
• (Optionally) to run a Python program from a command line
Note: Don’t worry if you can’t finish the entire lab exercise. Submit as much as you’ve completed to
Canvas before you leave the lab. Finish the rest of the lab on your own time.
Before you start, please sign the attendance sheet. Also, open an edit window in IDLE to create a
file named Lab5.py. Develop all of the functions of this lab in this file, including any test code.
Splitting a string
Consider a string representing the time of day — for example, '10:55:39' which means 39 sec-
onds after 10:55 AM. In this exercise, you will first pick apart a string such as this and then convert
hours to minutes and minutes to seconds to get the time in seconds after midnight.
To do this, you will split a string into one or more substrings using the split() string method.
1. To learn about the split() method, select the Help > Python Docs menu command of
IDLE. The Python Documentation window should open. The panel at the left of the window con-
tains four tabs — Contents, Index, Search, and Favorites. Under Index, type split. The index will
scroll to the “split” entries in the index. Select “(str method)” to bring up the documentation
page for the string method named split. Read and study this description. 1
2. Write a function called timeToSeconds(time) that takes a string of the form
hh:mm:ss
and converts it into the number of seconds since midnight. You may assume that 0 ≤ hh < 24,
that 0 ≤ mm < 60, and that 0 ≤ ss < 60. Your function must return an integer value.
3. Write second function called secondsToTime() that converts the number of seconds since
midnight into a time string of the form accepted by part 2 above. Your function must return this
string.
1 A brief description of the split() method can also be found on page 136 of the textbook.
Parse a date
In this exercise, you will use the string method find() to help you to parse a date.
5. Learn about the find() string method in the Python documentation. 2
6. Write a function called parseDate(dateString) that returns a list of three integers repre-
senting the month, day, and year of the date represented by the dateString argument.
You may assume that the date string contains exactly the following:– a month name spelled cor-
rectly and capitalized in English) followed by a space, followed by the day (an integer), followed
by another space, followed by the year (also an integer).
Although it is possible to convert month names to numbers using a long sequence of if and
elif statements, in this Lab, you must use the find() string method.
To do this, set up a constant string of the form
JanFebMarApr…
containing the first three letters of the names of the months, concatenated together in order.
When presented with an input date, slice off the first three letters of the input month, and then
use the find() string method to find the position of those first three letters in your constant
string. Finally, work from that position to determine the month number.
Write a test function to prompt the user for input dates and confirm that they are correct.
7. If you still have time, modify your parseDate() function to ignore the case of the input
string. Call the modified function parseDate2(). Find and use the appropriate string meth-
ods to deal with the case of the string.
If you wish to try the extra credit part of Homework #5, read on. Otherwise, you are done for this
lab. Be sure to save your Lab5.py file and submit it to Canvas under the assignment Lab5.
2 The find() method for strings is mentioned briefly in Table 5.2 on page 140.
if __name__ == '__main__':
printArgv()
This will cause the printArgv() function to be called before the window is displayed when-
ever Lab5.py is run. Try it to see what is printed in the shell.
10. Finally, let’s try to invoke Lab5.py directly from a command prompt and let it display the ar-
guments from the command line.
Open a Command Prompt (i.e., a Terminal on the Macintosh).
Use the cd command change directory to the directory/folder where you stored Lab5.py and
graphics.py. If you need to list a directory, use the dir command in Windows or the ls
command in Macintosh.
Once you are in that directory/folder, type the following command on one line:–
Figure 1
3 Environment variables are variables that operating systems keep in order to record useful information and locations
where useful stuff is kept. The system-wide environment variables are shown in the lower panel of Figure 1, and the
user-specific environment variables are shown in the panel window. The Path environment variable is a list of direc-
tories, separated by semi-colons, that name the directories where the operating system searches for named programs
to run. By adding C:\Python35, you have just told the system to also look there for programs to run. The python
program is located in that directory (actually, it is named python.exe.)