CS21 Lab 1: First Programs

Due Saturday, September 19, 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. Mad Libs

Mad Libs is a word game that uses a template and asks the player to give words of certain types to fill in the template, often leading to humorous results. In the file madlibs.py, you will use a short template to create a poem about cs21.

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

$ python3 madlibs.py
Let's make a Mad Libs poem!

Enter a plural noun: cats
Enter another plural noun: dogs
Enter a color: orange
Enter another color: blue
Enter an adjective: awesome

cats are orange
dogs are blue
cs21 is awesome
and so are you

$ python3 madlibs.py
Let's make a Mad Libs poem!

Enter a plural noun: noodles
Enter another plural noun: bananas
Enter a color: purple
Enter another color: green
Enter an adjective: squishy

noodles are purple
bananas are green
cs21 is squishy
and so are you

Notice that the template for our Mad Lib is:

PLURAL_NOUN1 are COLOR1
PLURAL_NOUN2 are COLOR2
cs21 is ADJECTIVE
and so are you

2. Ride Share Costs

In the file rideShare.py, write a program that simulates a ride-sharing app that calculates the cost for each rider on a group trip. Your program will ask the user for the miles traveled, the number of passengers, and the tip percentage. The fees for this service are $1.25 per mile and $2.00 per passenger. You will output the subtotal before the tip, the tip amount, the total cost, and the cost per passenger.

Here are two examples of how your program should work. Again user input is in bold.

$ python3 rideShare.py
Welcome to the Ride Share App.
Miles traveled: 15
Number of passengers: 2
Tip percentage: 20
--------------------------------
Subtotal: $ 22.75
Tip: $ 4.55
Total: $ 27.3
Per passenger: $ 13.65

$ python3 rideShare.py
Welcome to the Ride Share App.
Miles traveled: 10.5
Number of passengers: 1
Tip percentage: 15
--------------------------------
Subtotal: $ 15.125
Tip: $ 2.26875
Total: $ 17.39375
Per passenger: $ 17.39375

You do not have to worry about having an even number of pennies (i.e., exactly two decimal places). We will learn how to do that later in the semester.

You should assume that the number of passengers and the tip percentage are whole numbers (integers), and that the miles traveled is real valued (float).

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