Exam 2 Lab Problem

This lab project involves several steps. Work through them in order. In the files you create, it would help both of us if you add comments indicating where you are in this sequence of steps.

Before you begin work on this project, set up a directory named “ExamLab2” in the homework repo on your system and do all your work in that directory.

Note

As you work through this program, it will help if you add output statements showing what is going on. Build the program in steps, checking your work as you go. All of the functions you create can show what they did with a simple print statement. When you get ready to turn the code in, comment out the extra print statements (do not remove them) so I can see how you tested your code. I will give extra credit for good testing.

Step 1: The main program file

You need to set up a simple program file named “Exam2.py” with a function named “main” that prints out this set of lines:

Exam 2 Lab problem
Submitted by: Firstname Lastname
On November 18, 2018

Obviously, you should change some of this output. If you submit a program that prints out exactly what is shown above, I will give the credit to someone named “Firstname Lastname”!

Your file should have a call to the main function you created. That call should be in the final if block commonly used in in python code files. Test your file to make sure it generates the required output.`

Step 2 - Setting up a module

Next, set up a second file named mymath.py. In that file do the following:

  1. Add the line that makes all of the functions in the Python standard math module avaliable.

  2. Write a function named “powerval” in your module file that takes two integer parameters. You can name these parameters anything you like.

  3. Your function should examine the two parameters to see which is the smaller of the two, storing that value in a variable named small and storing the larger of the two in a variabel named big.

  4. The function then calls the pow function from the Python “math” library to evaluate the value of big raised to the small power. Your function should return that value to the caller. Make sure this number is an integer (you will need to force it to be an integer using a standard Python technique).

Step 3 - Reading data from a file

Create a text file named “sample.dat” with the following data items, one per line:

  • 1 2 4 7 2 4 9 3 8 2 3 8 10 2 -1

Now do the following:

  1. In your main program file, add a function named “getData” that opens the “sample.dat” file for reading.

  2. Set up a variable named done and initialize that variable to False

  3. Set up a while loop that uses the variable done as the looping condition. It will look like this:

done = False
while not done:
    # your code goes here

Does this make sense? We will be stuck in this loop until we decide we are done. Then we will set the variable done to True and it will stop!

  1. Inside the loop body, read in a number from the file. If that number is negative, set the variable done to True and exit the loop. If that number is not negative, read in another number. Then call the powerval function (in your module file) passing the two numbers you have read as parameters. When you get your value back from the powerval function, print that number on the console followed by an end of line marker (“n”). No other output is required.

  2. Add a call to your getData function inside your main function, after the output lines from step 1.

Test this version of your program and make sure it displays the right values for each pair of numbers from your data file. You should see 7 numbers.

Step 4: Summing up numbers

In this segment, we will modify the getData function so it writes out the numbers we previously saw on the console into an output file instead.

  1. In your getData function, open up an output file named “output.dat”. Change the output code you have there now so it writes the number to the output file, one number per line.

  2. Add code to close the output file when the processing is compete

  3. Run your program and check that you see the same seven numbers in the file, and that they are on a single line each.

  4. Set up a new function in your main program file named summer that takes no parameters.

  5. Open your “output.dat” data file as an input file.

  6. Set up a loop that reads all the numbers from the input file and adds them up. When you get done, return that final total to the caller of the summer function.

Step 5 - Completing the main routine

Your main program code should have a call to the “getData” function. Add a call to the summer function right after this call, and save the return value in a variable. Finally, print out the final sum on a line that looks like this:

The final sum was xxx

Note

Hint: The answer I got was 3824

Congratulations, you are done. You will need to push your repo to GitHub. Make sure you have these two Python source files:

  • Exam2.py

  • mymath.py