CS21 Lab 1: First Programs
Due Saturday, February 1, before 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.
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
input()
and output (print()
) -
get comfortable with python data types:
int
,float
,str
1. favorite color
Write a program called favcolor.py
that does simple string input and output
with the user. Your program should first ask for the user’s name, then
output a simple greeting (see below for the format). Then ask for the
user’s favorite color, and again output a simple statement using the
given color (see below).
Two examples of the running program are shown below. User input is shown in bold.
$ python3 favcolor.py
What's your name? Jeff
Hi, Jeff !
What is your favorite color? blue
Cool. I like blue too!
$ python3 favcolor.py
What's your name? Lisa
Hi, Lisa !
What is your favorite color? royal azure
Cool. I like royal azure too!
In the first example run, the user enters Jeff
for the name
and blue
for the favorite color.
2. running times
Write a program called runner.py
that calculates 1-mile and
10-kilometer (10K) running
times for the user. Your program should first ask the user how long it
takes them to run one lap on a track (in seconds), then calculate the running times
(in seconds and minutes)
based on their input. One mile is 4 laps on a track, so multiply the
input time by 4 to get the runner’s mile time. Ten kilometers is 6.2
miles.
Two examples of the running program are shown below. User input is shown in bold. Note the second example, where the use enters a float!
$ python3 runner.py
seconds for 1 lap on track? 120
1 mile time: 480.0 seconds (minutes = 8.0 )
10K time: 2976.0 seconds (minutes = 49.6 )
$ python3 runner.py
seconds for 1 lap on track? 137.9
1 mile time: 551.6 seconds (minutes = 9.193333333333333 )
10K time: 3419.92 seconds (minutes = 56.998666666666665 )
3. tip calculator
Write a program called tipcalc.py
to suggest the amount to leave for a tip, given
the total cost for a dinner. Your program should ask the user for the total cost,
then calculate and display three possible tips: 10%, 15%, and 20%.
Two examples of the running program are shown here. User input is shown in bold.
$ python3 tipcalc.py
total bill? $100
with 10% tip: $ 110.0 (tip = $ 10.0 )
with 15% tip: $ 115.0 (tip = $ 15.0 )
with 20% tip: $ 120.0 (tip = $ 20.0 )
$ python3 tipcalc.py
total bill? $37.59
with 10% tip: $ 41.349000000000004 (tip = $ 3.7590000000000003 )
with 15% tip: $ 43.228500000000004 (tip = $ 5.6385000000000005 )
with 20% tip: $ 45.108000000000004 (tip = $ 7.518000000000001 )
3.1. Extra Challenge — string formatting
This part does not affect your grade so please only attempt this after completing the rest of your lab. It is simply an extra challenge, if you want to try it. And we will cover string formatting in the next few weeks, so even if you don’t have time for this now, we’ll get to it later.
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:
$ python3 extra.py
total bill? $37.59
with 10% tip: $ 41.35 (tip = $ 3.76)
with 15% tip: $ 43.23 (tip = $ 5.64)
with 20% tip: $ 45.11 (tip = $ 7.52)
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, 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 are all done working in the lab, you should log out of the computer you are using. First quit any applications you are running, like the browser and the Terminal. Then click on the logout icon and choose "log out".
If you plan to leave the lab for just a few minutes, and then come right back to work, 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 icon.