Lab 2

Introduction:

In this lab, you will:

  1. Refer to the Python documentation to learn how to use a function.
  2. Practice getting input from the user.
  3. Practice string searching.
  4. Practice math operations.

Part 1: Find the second occurrence in a string

Reading Documentation

The find and index functions in strings can be used to find the location of a string inside another string. In lecture, only finding the first occurrence of a string was presented, but both functions can be used to find more than just the first occurrence. Read the Python documentation for the find function to learn how to start searching after a certain position in the string: https://docs.python.org/3/library/stdtypes.html#str.find.

Program

Write a program that:

  1. Asks the user for text.
  2. Asks the user for a search string (i.e. the text to find in the sentence).
  3. Find the first occurrence of the search string.
  4. Using the location of the first occurrence and what you learned about the find function from above, find the location of the second occurrence.
  5. Print out the starting location of the first occurrence.

An example program:

Please enter a sentence: she sells seashells by the seashore
Please enter a search string: ell
The second location of 'ell' is 15.

Question

Answer the following question on your assignment information sheet.

  1. What could cause your program to crash, throw an exception/error, or not behave as the user might expect?

Part 2: Numeric Computation

The distance an object moves as it falls can be described by the equation:

d = 4.9*t2

where d is the distance and t is the time it has been falling.

Program

Write a program that:

  1. Asks the user for the time the object has been falling.
  2. Computes the distance.
  3. Displays the distance (to two decimal places, even if one of them is zero as in the example below) and some descriptive text explaining what the result is.

An example program is:

This program will find the distance an object has fallen given the time it has been falling.
Please enter the time (in seconds) it has been falling: 4
The object will have fallen 78.40 meters.

Use a function from the math module to compute the square of the time (i.e. t2).

Question

Answer the following question on your assignment information sheet.

  1. What kinds of inputs could cause your program to crash, throw an exception/error, or not behave as the user might expect? There are at least three kinds. How could you use what we talked about in class on Wednesday to help deal with these problems?

Submission and Grading:

Complete the Assignment Information Sheet.

Submit your final program and assignment information sheet (zipped into one file) to CourseWeb in the Lab 2 assignment.

The grading rubric can be found here: Rubric (doc).

The assignment is due Monday, May 25 by 11:30 am. As with all programming assignments, you have unlimited uploads (before the deadline), so you may upload the assignment before the deadline. If you later decide to upload another, you may do so without penalty (as long as it's before the assignment deadline). The last submission uploaded before the deadline will be the one graded. If you would like ungraded feedback on a programming assignment, you may send an email to your TA or the instructor and ask for feedback; please send your code as well.

For more advice on submitting your assignment, see the Programming Assignments section of the Tips for Success page.