CS21 Lab 1: First Programs

Due Saturday, January 29, by 11:59pm

In this lab you will write a few basic python programs. For each program, we provide specifications and some sample output. Edit the files after running update21 and then run handin21 to submit your solution. Complete a short questionnaire once you are done. Full details are below.

Programming Tips

As you write programs, use good programming practices:

  • Use a comment at the top of the file to describe the purpose of the program (see example).

  • All programs should have a main() function (see example).

  • Use variable names that describe the contents of the variables.

  • Write your programs incrementally and test them as you go. This is really crucial to success: don’t write lots of code and then test it all at once! Write a little code, make sure it works, then add some more and test it again.

  • Don’t assume that if your program passes the sample tests we provide that it is completely correct. Come up with your own test cases and verify that the program is producing the right output on them.

  • Avoid writing any lines of code that exceed 80 columns.

    • Always work in a terminal window that is 80 characters wide (resize it to be this wide)

    • In vim, at the bottom left in the window, there is an indication of both the line and the column of the cursor.

Are your files in the correct place?

Make sure all programs are saved to your cs21/labs/01 directory! Files outside that directory will not be graded.

$ update21
$ cd ~/cs21/labs/01
$ pwd
$ ls
(should see your program files here)


The goals for this lab assignment are:

  • Write your first python programs!

  • Practice using an editor

  • Use variable assignment to store values

  • Use print() and input()

  • Learn to identify and fix common errors in programs

1. A first program

The file intro.py is initially empty. Practice using an editor and type in the program below. After saving your file, run the program and fix any errors.

Here is an image of the program to type.

First Program

Here are two examples of the running program. User input is shown in bold.

$ python3 intro.py

What is your name? Tamika
Nice to meet you
$ python3 intro.py

What is your name? Matty Bevers
Matty Bevers
Nice to meet you

2. Basic math operators

Complete the program started in ops.py to print the result of adding and multiplying two numbers x and y stored as variables. In this example program, you should not prompt the user to type in the numbers. The values can be stored directly in the program as variables. You may choose an arbitrary value for y between 1 and 10. A sample run is shown below when y is set to 3. User input is shown in bold.

$ python3 ops.py
x+y =
x*y =

It is perfectly fine to print x+y = on one line and the result on the next line as shown above. We will learn formatted output in a week or two to have the text and the result appear on the same line.

What is the differrence between print("x") and print(x)? How can you use this to solve the problem?

3. Python debugging

Often a program won’t work as intended the first time we try to run it. We must review and revise the program in a process called debugging to fix syntax or logic errors in our program. The provided program fixme.py is supposed to prompt the user to type in a phrase and print it out twice. However when we run the program, we get the following output:

$ python3 fixme.py
  File "fixme.py", line 13
    resp = input("Type a phrase: )
SyntaxError: EOL while scanning string literal

Look closely at the program and the error output and try to fix this first error. Looking at a working example program may help. Unfortunately, there are two more errors in this program. After fixing the first, continue your debugging process to fix the other two. The error messages for the first two errors give pretty good hints. Before fixing the last error, your program might run as follow:

$ python3 fixme.py

The is no output at all, yet no error messages. Something is missing at the end of your program. Can you add it to fix the program? Compare the bottom of your non-working program to example program if you need a hint.

When all three errors are fixed, the program should run as follows, where user input is in bold:

$ python3 fixme.py
Type a phrase: It's working!
It's working!
It's working!

4. Answer the Questionnaire

Each lab will have a short questionnaire at the end. Please edit the Questions-01.txt file in your cs21/labs/01 directory and answer the questions in that file.

Once you’re done with that, you should run handin21 again.

Submitting lab assignments

Remember to run handin21 to turn in your lab files! You may run handin21 as many times as you want. Each time it will turn in any new work. We recommend running handin21 after you complete each program or after you complete significant work on any one program.

Logging out

When you’re done working in the lab, you should log out of the computer you’re using.

When Remotely logged in

When you are ssh’ed into the CS labs, first quit any applications you are running, like vim, then simply type exit at the prompt in your terminal window to disconnect.

When Physically logged in

When you are in a CS lab logged into a CS machine. First quit any applications you are running, like the browser and the terminal. Then click on the logout icon (logout icon or other logout icon) and choose "log out".

If you plan to leave the lab for just a few minutes, you do not need to log out. It is, however, a good idea to lock your machine while you are gone. You can lock your screen by clicking on the lock xlock icon. PLEASE do not leave a session locked for a long period of time. Power may go out, someone might reboot the machine, etc. You don’t want to lose any work!