This lab assignment requires you to write a few Python programs. Begin by entering the sequence of commands you see below in your Unix terminal. What you should type is shown in blue.
$ update21 [You'll see some output] $ cd cs21/labs/01 $ pwd /home/[your_username]/cs21/labs/01 $ ls README-01.txt
The goals for this lab assignment are:
For full credit on this assignment, you should:
For now, you can assume that your user always enters something that makes sense. Later in the semester, we'll teach you how to handle nonsensical user input.
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.
Write a program that constructs a mini Mad Lib. You should prompt the user for words that fall into various categories, and then plug the responses into a template of one or two sentences. If you ran this program three times, it might look like the following:
$ python madlib.py Person: David Verb (present tense): complaining Today's top story: David sets record in competitive complaining contest! $ python madlib.py Person: Jeff Verb (present tense): snoring Today's top story: Jeff sets record in competitive snoring contest! $ python madlib.py Person: Tia Verb (present tense): texting Today's top story: Tia sets record in competitive texting contest!
You can use the same template as us (Today's top story: [PERSON] sets record in competitive [VERB] contest!) or devise one of your own. Put your code in a file called madlib.py. To create this file and begin editing it in vim, enter the following command:
$ vim madlib.py
More on Mad Libs from wikipedia.
Write a program to help a party of friends split a restaurant bill equally. The program should ask for the total bill amount, the percent tip to add, and how many people are splitting the bill. Here is what two runs of your program should look like:
$ python splitbill.py Bill amount in dollars: 55.34 Percent tip: 20 Number of people: 3 The tip is 11.068 dollars The total cost is 66.408 dollars The cost per person is 22.136 dollars $ python splitbill.py Bill amount in dollars: 162.29 Percent tip: 15 Number of people: 8 The tip is 24.3435 dollars The total cost is 186.6335 dollars The cost per person is 23.3291875 dollars
Put your code in a file called splitbill.py. To create this file and begin editing it in vim, enter the following command:
$ vim splitbill.py
This does not affect your grade so please only attempt this after completing the rest of your lab
The output above is not realistic since money does not come in smaller denominations than 1 cent (i.e., we do not need more than two digits after the decimal point). Use string formatting to clean up the output like this:
$ python splitbill.py Bill amount: $162.29 Percent tip: 15 Number of people: 8 The tip is ............$ 24.34 The total cost is .....$ 186.63 The cost per person is $ 23.33
Write a program that prompts the user to enter an integer factor and then displays the first nine multiples of that factor. Here's how it should look when you run this program:
$ python multiplytable.py Factor: 5 n | 5 * n ---------- 1 | 5 2 | 10 3 | 15 4 | 20 5 | 25 6 | 30 7 | 35 8 | 40 9 | 45 $ python multiplytable.py Factor: 7 n | 7 * n ---------- 1 | 7 2 | 14 3 | 21 4 | 28 5 | 35 6 | 42 7 | 49 8 | 56 9 | 63
Put your code in a file called multiplytable.py. To create this file and begin editing it in vim, enter the following command:
$ vim multiplytable.py
Pro tip: if you get sick of typing out multiplytable.py you can type just the first couple letters and then press the Tab key to take advantage of Unix's autocomplete feature. Better yet, if you press the up arrow while in Unix, it will cycle through your previously entered commands.
Once you are confident that all three programs work, fill out the questionnaire in README-01.txt. Then run handin21 a final time to make sure we have access to the most recent versions of the four files required for this lab: madlib.py, splitbill.py, multiplytable.py, and README-01.txt. Unlike last week, this lab will be graded.
When you run handin21 you should see a list of the files being submitted. If you submit all four files at once, it should look something like this:
$ handin21 Submitting the following files: labs/01/README-01.txt labs/01/madlib.py labs/01/multiplytable.py labs/01/splitbill.py