CS 115 Lab 5, Part A: Print points

[Back to lab instructions]


Instructions

  1. Review the description of peaks and valleys in the pre-lab instructions.
  2. Create a new Python source code file named lab05a.py:
    """
    Program: CS 115 Lab 5a
    Author: Your name
    Description: This program reads a bunch of y-values from a file.
    """
    
    
    def main():
        # Opens the input file - you don't have to understand this line
        pointsfile = open("points-test.txt", "r")
    
        # Reads and prints the first line of the input file
        num_points = int(pointsfile.readline())
        print('Number of points:', num_points)
    
        # Reads and prints the points
        for i in range(0, num_points):
            y = int(pointsfile.readline())
            print('Point ', i + 1, ': ', y, sep="")
    
        pointsfile.close() # Closes the input file after reading it
    
    
    main()
  3. This program simply reads and prints the set of points. Since it would be tiresome to enter these points one at a time, we are reading them from one of the text files you downloaded (points-test.txt). The first line of the text file is the number of points to read, and the remaining lines are the y-values of the different points.

  4. Run this program, and answer Question 1 in your writeup. Call for help if the program crashes because it can't find the input file.
  5. Continue to Part B.