CS21 Lab 1: First Programs

Due Saturday, September 11, by midnight

Programming Tips

As you write your first programs, start using good programming practices now:

  • 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.

    • Atom shows the column number in the lower left and draws a vertical line down the editing window at 80 characters.

    • In emacs, at the bottom, center of 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
/home/username/cs21/labs/01
$ ls
Questions-01.txt
(should see your program files here)

Goals

The goals for this lab assignment are:

  • write your first python programs!

  • get comfortable with using input() and print()

  • get comfortable with python data types: int, float, str

  • use type casting with input() to get numeric data

1. Python Math Operators

To familiarize yourself with python’s math operators, write a program called operators.py that asks the user for two integer operands and shows the results of applying:

  • Addition (+)

  • Multiplication (*)

  • Division (/)

  • Integer division (//)

  • Exponentiation (**)

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

$ python3 operators.py
Please enter two integers.

First integer: 3
Second integer: 7

3 + 7 = 10
3 * 7 = 21
3 / 7 = 0.42857142857142855
3 // 7 = 0
3 ** 7 = 2187
$ python3 operators.py
Please enter two integers.

First integer: 8
Second integer: 2

8 + 2 = 10
8 * 2 = 16
8 / 2 = 4.0
8 // 2 = 4
8 ** 2 = 64

Note: you can use print() with no arguments to print a blank line.

2. Digital Haiku

Computational poetry is an interesting and creative art form.

Writing poetry is hard — fortunately, we have computers to help us out. For the next program, haiku.py, you’ll use a short template to generate digital haiku poetry. Since this program is essentially a Mad Lib, it’ll be up to the user to ensure the proper number of syllables (your program should not try to enforce syllables)!

Here are two examples. User input is shown in bold.

$ python3 haiku.py
Digital Haiku
Enter a noun: fall
Enter an adjective: cool
Enter a noun: homework

a new fall arrives
  the cool air begins to blow
    homework will come next
$ python3 haiku.py
Digital Haiku
Enter a noun: doge
Enter an adjective: much
Enter a noun: wow

a new doge arrives
  the much air begins to blow
    wow will come next

Note that the template for our haiku is as follows (and watch the spaces!):

a new NOUN arrives
  the ADJECTIVE air begins to blow
    NOUN will come next

3. Tip Calculator

Not sure how much to tip your waiter? Python can help! For the last program this week, you’ll write a tip calculator program, tips.py. Given a decimal number input representing a restaurant bill, tips.py should report tip amounts for 10%, 15%, and 20%, along with the totals. Note: you are not required to format decimals to round to the nearest penny. We haven’t learned how to do that yet.

$ python3 tips.py
How much was the bill? 10.00

10% tip: $1.0, Total: $11.0
15% tip: $1.5, Total: $11.5
20% tip: $2.0, Total: $12.0
$ python3 tips.py
How much was the bill? 22.39

10% tip: $2.2390000000000003, Total: $24.629
15% tip: $3.3585, Total: $25.7485
20% tip: $4.478000000000001, Total: $26.868

3.1. Optional Rounding

As an optional challenge, you can use Python’s built-in round() function to round your results to the nearest penny. To use it, you can call round with two parameters: the number to round and the number of digits you want to round to (two for pennies).

For example, if you wanted to round pi to two decimal places:

pi = 3.141592

pi_rounded = round(pi, 2)

print("pi rounded to two decimal places is: " + str(pi_rounded))

Which produces:

pi rounded to two decimal places is: 3.14

Note that converting the result of round to a string with str() is necessary for concatenation (adding the result to a string).

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.

Turning in your labs…​

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. 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!