CS21 Lab 1: First Programs

Due Saturday, February 20, 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. 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. checkpoint

At the end of your lab session, be sure to run handin21.

Your lab instructor will check your files, which should reflect that you have made non-trivial progress (in python or pseudocode) towards the solution. Note that if you have not made much progress towards your solution, we expect that you would have been actively seeking help during your lab over Slack.

10% of your lab grade is dependent on this checkpoint.

If there are circumstances which prevent you from making substantial progress on this lab, please contact your lab instructor as soon as possible.

2. Digital Poem

Computational poetry is an interesting and creative art form. Here’s one of my favorites, which is composed of randomly glued toether Tweets that happen to be in iambic pentameter. One way to think about digital poems is that they can lean into the algorithmic nature of programming rather than trying to emulate what humans might write naturally. Nonetheless, the results are often beautiful or through provoking. In the file digitalpoem.py, you will use a short template to create your first digital poem.

Two examples of the running program are shown below. User input is shown in bold.

$ python3 digitalpoem.py
Let's craft a computational poem.
Enter a color: sunset red
Enter an adjective: peaceful
Enter a noun: meadow lark
Enter an emotion: serenity
--------------------------------
    sunset red leaves,
        peaceful waves of grain,
    	    the buzz of the meadow lark,
serenity....

$ python3 digitalpoem.py
Let's craft a computational poem.
Enter a color: darkest black
Enter an adjective: brilliant white
Enter a noun: past
Enter an emotion: acceptance
--------------------------------
    darkest black leaves,
        brilliant white waves of grain,
    	    the buzz of the past,
acceptance....

Notice that the template for our computational poem is as follows (and watch the spaces!):

    COLOR leaves,
        ADJECTIVE waves of grain,
    	    the buzz of the NOUN,
EMOTION....

3. Temperature Conversion

In the file temperature.py, write a program that can be used to convert from Fahrenheit to Snowman other temperature scales; in this case Celsius, Kelvin, and the Rankine scale.

Here are two examples of how your program should work. Again user input is in bold. To get the results shown below, you should convert the user input from a string into a float.

You can assume that the user enters a number.

$ python3 temperature.py
Welcome to the Temperature Converter.
Temperature in Fahrenheit: 32
--------------------------------
Fahrenheit: 32.0
Celsius: 0.0
Kelvin: 273.15
Rankine: 491.67

$ python3 temperature.py
Welcome to the Temperature Converter.
Temperature in Fahrenheit: 72
--------------------------------
Fahrenheit: 72.0
Celsius: 22.22222222222222
Kelvin: 295.3722222222222
Rankine: 531.6700000000001

Don’t worry about all of the digits after the decimal point. We will learn how to nicely format our output later in the semester.

Here is how to convert from Fahrenheit to the other temperature scales:

celsius = (fahrenheit - 32) * 5/9
kelvin = ((fahrenheit - 32) * 5/9) + 273.15
rankine = fahrenheit + 459.67

4. Coin Collecting

Edit the file coinvalue.py to create a program that calculates the value, in cents, of a collection of coins (as input by a user).

Here is an example of how your program should work (like one of these!) Again user input is in bold. To get the results shown below, you should convert the user input from a string into an int.

You can assume that the user only enters numbers.

$ python3 coinvalue.py
Welcome to the coin value app.
Enter number of quarters: 4
Enter number of dimes: 5
Enter number of nickels: 4
Enter number of pennies: 2
--------------------------------
Total cent value: 172

$ python3 coinvalue.py
Welcome to the coin value app.
Enter number of quarters: 0
Enter number of dimes: 100
Enter number of nickels: 4
Enter number of pennies: 7
--------------------------------
Total cent value: 1027

Reminder: Quarters are worth 25 cents, Dimes are worth 10 cents, Nickels are worth 5 cents and pennies are worth 1 cent.

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