Lab 2: Loops and Formatting

Goals

After the lab, you should know how to

  1. log into another Linux machine and execute commands
  2. format output nicely in Python
  3. solve different types of looping problems in Python

Lecture Slides

Read through today's lecture slides.

Objective: Practice Using Linux

Modifying Your Prompt

To make it a little easier for you to know what directory you are in when you're using a terminal/shell (i.e., using the command-line interface), we're going to update a customization file.
  1. In your home directory, open the file .bashrc with jEdit.
    Note that since the file name begins with a ".", it is hidden from jEdit. You will have to explicitly type the name of the file in jEdit's browse window or use jedit .bashrc to open the file.
  2. At the bottom of the file, add the following line:
    PS1='[\u@\h \W]> '
  3. Save the file.
  4. In jEdit, open .bash_profile
  5. Add the following line at the end of the file:
    export PS1
  6. Save the file.
  7. You now need to update the shell with the changes to your profile. Use the command:
    source .bash_profile

    You should see a change to your prompt. It should look something like:
    [username@machine ~]$

    The ~ represents your home directory. When you change directories, your prompt will reflect those changes.

    From now on, whenever you open a new terminal/shell, your prompt should look like this.

Set up for Lab 2

  1. Create a directory called lab2 in your labs directory. Your programs and the output for this lab will all be saved in the lab2 directory.
  2. Copy the files from /home/courses/cs111/handouts/lab2 into your lab2 directory.

Linux: A Multi-user OS

Linux is a multi-user operating system. You can log in to another machine and run your programs there. However, you have to use the command-line interface on the other machines.
  1. Log into/ssh into the machine in the front left corner of the room (named modula) by using the command:
    ssh modula

    Say yes when it asks you about the RSA fingerprint.

    Enter your password.

  2. Run the command who to see who else is logged into the machine. Copy the output from this command into a file called "linux_practice.txt" in the lab2 directory. Label the output appropriately.
  3. Run the command top to see how different users are using the machine's resources. In the file linux_practice.txt, note about how much CPU and Memory is being used and how the resource usage changes as other people login and use modulo as well.
  4. Read the sum_square.py program using the command:
    cat sum_square.py
  5. Execute the sum_square.py program.
  6. Run the command top again to see if users are using the system differently. Note any observations in linux_practice.txt

Objective: Programming in Python

We'll practice writing several Python programs, each in their own text file. Name the files lab2.1.py through lab2.6.py.

Your programs will be graded on correctness, style, and how well you tested them. Make sure you adhere to the good development and testing practices we discussed in class. Your code should be readable and your output should be useful and well-formatted.

After you've developed a correct solution to each program, restart IDLE or close and reopen the IDLE "shell" by running the program again (using F5), demonstrate that the program works using several good test cases, and save the output to a file named lab2.x.out, where x is the problem number.

  1. (15) On the first day of class, Laura and Jennifer said that they play tennis. Jennifer can hit the tennis ball across the 78-foot court in .6 seconds. Laura, a little younger, hits the ball across the court in .7 seconds. Venus Williams hits the ball across the court in .5 seconds. Calculate how fast they are hitting the ball in ft/s and mi/hr. The output should be nicely formatted, similar to what is shown below:
        CISC111 Tennis Pros
    -----------------------------
                 ft/s    mi/hr      
    Laura        ----     ----
    Jennifer     ----     ----
    Venus        ----     ----
    
  2. (15) Write a program that sums all the odd integers between 0 and 100 and prints the total. Use a defined constant to stop your loop.
  3. (15) Using three variables (i, j, and result), assign them values to calculate and display result = i % j. Use a series of assignment and print statements and a for loop to show, in a single execution, the results of i % j, where i = 4 and j increases from 1 to 8.
  4. (15) The Fibonacci sequence is 1, 1, 2, 3, 5, 8, 13, ... The pattern is that the nth number is Fn=Fn-1 + Fn-2. The sequence is defined that F0=F1=1. Write a program that computes the first 15 numbers in the Fibonacci sequence. (If you're having difficulty solving this problem, think about how would you solve this problem by hand, calculating and writing out the results. You won't receive any help until we see that you have something written out.)
  5. (20) Write a program to write the multiplication tables from 0 to 12.

    Example output:

    x | 0 | 1 | 2 | 3 | 4 |  5 |  6 |  7 |  8 |  9 | 10 | 11 | 12 |
    ---------------------------------------------------------------
    0 | 0 | 0 | 0 | 0 | 0 |  0 |  0 |  0 |  0 |  0 |  0 |  0 |  0 |
    1 | 0 | 1 | 2 | 3 | 4 |  5 |  6 |  7 |  8 |  9 | 10 | 11 | 12 |
    2 | 0 | 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 | 20 | 22 | 24 |
    
  6. (15) Using two for loops, a variable with value "She loves you," and another variable with value "yeah", print out the Beatles lyrics:
    She loves you, yeah, yeah, yeah
    She loves you, yeah, yeah, yeah
    She loves you, yeah, yeah, yeah
    Yea-aahh
    

Finishing up: What to turn in for this lab

  1. Copy your lab2 directory into the turnin directory. (Review the UNIX handout if you don't remember how to do that.)
  2. Cleanup: jEdit makes backup files and appends "~" to the name of your file. Delete any "~" files from your lab directory.
  3. Turn in your printed lab assignment, using the printLab.sh command:
    printLab.sh <labdirname>

    Again, you should probably print from the labs directory.

    Print the file using the lpr command introduced in the first lab.

Labs are due at the beginning of Friday's class. You should hand in the printed copy at the beginning of class, and the electronic version should be in the turnin directory before 2:25 p.m. on Friday.

Ask well before the deadline if you need help turning in your assignment!

Grading (100 pts)