Lab 1: Linux, HTML, Python Programming, and Numeric Operations

Table of Contents:

Goals

After the lab, you should know how to

Objective: Preparing for Lab

Review Lab 0 if you don't remember how to remotely access the lab machines.

If you're logging in remotely, preferably jump to one of the machines in the middle of the row (logo, postscript, rexx, smalltalk, apl, cilk, forth, limbo), where no one will sit. You will need to accept the key for the machine if you haven't ssh'd to it before.

After logging into the lab machine in person and opening a terminal OR remotely logging into fred and then hopping to another machine, run runHelpClient.

Objective: Learning to Use Linux

We are learning how to use the Linux Machines in class (the slides).

For reminders of Unix commands, check out the CS dept wiki's page on basic Unix commands.

Objective: Using a Text Editor and Creating an HTML File (20)

We're going to create a web page using a simple text editor. Creating web pages isn't all or even most of what a computer scientist does. However, the Web is a great venue for a computer scientist to share her work with the world! (Or to create resources and information for her students.)

Use you're newly learned Unix command skills to do the following:

  1. If you're not already in a terminal, open a terminal.
  2. Go into your public_html directory. This is where you will put files that you want others to see on the Internet.
  3. Copy the example.css, example.html, and example.gif files from /csdept/courses/cs111/handouts/lab0 into your public_html directory. (Recall: what is the special name for the current directory? How can you copy all three files using one command?)
  4. Open another tab in your browser so that you can switch between the lab instructions and your work. Point your browser at the location: http://cs.wlu.edu/~yourusername/example.html
    You should see the example web page. If you get an error message in the browser window, ask for help. If no one is around and the error message is a "permission denied" message, try executing this command in a terminal, in the public_html directory:
    chmod o+r *
    and then reloading the page. This command means "change modifiers" and is giving others read permission on those files.
  5. Point your browser at the location: http://www.cs.wlu.edu/~yourusername/
    What do you see?
  6. Copy example.html into a file named index.html. Now, point your browser to the location: http://www.cs.wlu.edu/~yourusername/
    What do you see?

    (For now, just recognize that index.html hides the contents of your web directory and is the default file that the web server "serves" when someone accesses a directory.)

  7. Type jedit index.html &

    The & (called the ampersand) is a special command to let the terminal know that you want the program to open in a new window and lets you keep using the terminal window too. I will also refer to the ampersand as saying that you want the command to "run in the background".

    jEdit is a text editor, i.e., a program that helps you create files. jEdit looks similar to a word processor. Look through some of the menus and you'll notice a lot of functionality that you'd see in a typical word processor, such as save, open, copy, and paste.

  8. Edit the file so that the title (between the <title> and </title> tags) and the top-level heading (between the <h1> and </h1> tags) say Your Name's Web Page instead of Example Web Page. Save the file. Reload the page in the browser.
    How cool is that?

    Note that the page has several links to other online documents. The HTML Help link is useful if you want to develop more web pages after this brief introduction.

  9. In your web browser, view the source for the web page. You can do this by right-clicking on the page and then you'll see "View Page Source" or something similar. Viewing the source is another way to help you find errors.

    In another tab in your browser, go to the W&L web page or another page of your choice, and view its source. Anyone can view any page's HTML source!

  10. Add two more links to the page.
    1. The first link will be to the CSCI111 student sitting to your right--perhaps that person is across the aisle. If you're in the rightmost chair, you'll link to the leftmost person in the row behind you. If you're the back, rightmost person, you'll link to the leftmost person in the first row.

      If you're remote, we'll talk about the link ring you're making.

      Find out the person's full name, username, and year to create the link. The text for the link should include the student's full name and year, e.g, first-year, sophomore, too. In the end, we will have created a "ring" of CSCI111 students.

    2. The second link will be to some online news article about computer science. See the Extra Credit page for ideas on how to find such an article. (You should not simply take one of the articles I have linked from that page.) You may want to search for an article after lab, depending on your time constraints because we still have a lot to do in this lab.

    Note the pattern of the links in the HTML file. How would you create a link to CNN? (You will perform similar activities throughout the course. You'll look at example code that does something similar to what you want to do, copy the code, and modify the code to do what you want.)

  11. Next, add an image to your web page. Find an image from the web. Save the image into the appropriate directory. Change the image (img) tag in your web page to display this image. The alt tag is used by screenreaders to describe the image to blind people.
  12. Don't forget to update the information at the bottom of the page as well, to give yourself credit for your work.

    Note that you didn't need any fancy software to create a nice web page! I create my web pages for the course in a similar manner.

Objective: Set up for Programming

  1. Open a terminal.
  2. If you closed your help client, run runHelpClient to help us know who to help next.
  3. Create a directory called lab1 in your cs111 directory. Your programs and the output for this lab will all be saved in the lab1 directory.
  4. Copy all the files from the /csdept/courses/cs111/handouts/lab1/ directory that end in .py into your lab1 directory.

Objective: Introduction to the Python Interpreter (15 points)

On our system, you have two options for writing Python programs:

  1. write your programs in jEdit and use the terminal to run Python. This is what I will tend to do in class.

    OR

  2. write your programs in IDLE, using its shell to run the programs and then restart the shell to demonstrate your program working.

For your lab work, use IDLE.

Using the Interpreter in Interactive Mode

In a terminal, navigate to your directory for this lab, which you set up earlier. (The directory should be ~/cs111/lab1.)

To open Python's integrated development environment IDLE, use the command idle3 &

Recall: what does the & allow you to do?

You can use the interpreter in interactive mode to try out expressions before using them in a script. IDLE labels the interactive mode as "Python Shell". In IDLE, the main window--opened by default on start--is the interactive Python interpreter.

Type the following expressions at the Python interpreter prompt, one line at a time, and note what the interpreter returns:

Save the output from the interpreter in a file called interactive_practice.out.
Make sure that this file is saved in the appropriate directory (~/cs111/lab1).

Using the Interpreter in Batch Mode

Save the output from the interpreter in a file called practice.out. Again, make sure that this file is saved in the appropriate directory (~/cs111/lab1).

Objective: Writing Your Own First Programs

Your programs will be graded on both correctness and style. Style will become more important as the semester continues.

As you probably figured out, the convention is that the names of Python scripts end in ".py".

Objective: Arithmetic Operations in Python

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

Your programs will be graded on correctness and style. Make sure you adhere to the good development practices we discussed in class, e.g., use good variable names.

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

If you need help, you can use the Python visualizer. Make sure to select "Write code in Python 3". This is the same tool that is used in the online textbook.

  1. (15 pts) This problem has several parts. You will create a program (i.e., a script) in the first step and modify it for each subsequent step. Note how the progression of steps for this problem adheres to the good development practices we dicussed in class. Submit one program and one output file. I'll know you did the steps from your comments.
    1. Create three variables (i, j, and result) to calculate and display result = i² + 3j - 5 for the case where i=7 and j=2. Your code will not look exactly like this formula. Display the result and verify that the result is what you expect. Consider if you were the user of the program and make the program display appropriate output. Put the answer/output in a comment.
    2. Change the name of the variable i to i21. Be sure to change the name everywhere the variable is used. Execute it to verify it still works.
    3. Now edit the Python script again and change the variable name to 21i. What error message do you get? Record in comments the error message and why the error message occurred.
    4. Revert your program back to a correct variable name.
    5. Make sure that a user looking at your program's execution would understand what the program is doing.
  2. (10 pts) How do you measure, measure a year? The musical Rent asks how you would measure a year and suggests love as the metric. Since we don't have a primitive data type that represents love, we'll measure a year in minutes. Write a program that computes (or calculates) the number of minutes in a year and plugs it into part of the chorus of Seasons Of Love.

    Note: do not recompute the number of minutes each time it is used in the chorus. Compute the minutes once and save it in a variable that you use in the print statements.

    Output should look like (which is not exactly the same as the actual song's lyrics):

    525600 minutes, 525600 moments so dear.
    525600 minutes - how do you measure, measure a year? 
    
  3. (15 pts) MLB Hall of Famers. Rickey Henderson was voted into the Major League Baseball Hall of Fame in 2009. At one time Henderson was the career leader in three major offensive categories: runs, stolen bases, and walks. (Barry Bonds passed him in walks.)

    Henderson stole 1406 bases and was caught 335 times. Lou Brock is second with 938 stolen bases and 307 caught stealing.

    Calculate and display Henderson's and Brock's successful base stealing percentages. (Hint: Divide the number of bases successfully stolen by the number of attempts.) Then, display the difference in their percentages.

    Note: The calculated numbers are not very pretty. We'll learn how to format the output more nicely later this term.

Finishing up: What to turn in for this lab

You should be in the terminal (not the Python shell) to execute the following commands. You should be in your cs111/ directory.

  1. If you're not already, go into your cs111 directory. Create the printable file of your lab using the command:
    createPrintableLab <labname>
    In this case, the lab name is lab1.

    Putting it all together, the command you should execute is
    createPrintableLab lab1

    The script createPrintableLab creates a condensed version of your lab in a postscript file called lab1.ps and then converts the file to pdf in lab1.pdf. To save paper, the script puts two pages on a piece of paper. The script also highlights the Python code appropriately.

    View the compiled printout using the command evince lab1.ps &
    evince allows you to view postscript files, which are suitable for printing. You should verify that the file "looks correct", i.e., no "garbage characters" and that your file is not be more than a few pages. Otherwise, you're printing too much. See the instructor or student assistant if you're having trouble.

    If you are working in the advanced lab, you may get an error about enscript not being installed (unless we have fixed it!). ssh to one of the machines in the teaching lab (remember: they are named after programming languages).

    Review your printout with Professor Sprenkle before moving on. OR, complete the next step and let her know that you have submitted your file so she can check it out.
    If you make changes to your files, you should run the scripts to create a printable lab and the turnin script.

    Cleanup: jEdit and IDLE may make backup files. The backup files have an "~" appended to the name of your file. When printing, if you see any files that end in ~, tell us and we will help you delete the files. Don't worry if you don't have these files.

  2. Submit your lab1 directory to your turnin directory so that I can see your files by running the command turnin.sh lab1
    If everything is set up correctly, you copied your lab1 directory PLUS the printable lab into your turnin directory. If you run the script again, it will create a backup of the previous submission and copy your current directory.

Labs are due at the beginning of Friday's class. 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 (95 pts)