CS21 Lab 1: First Programs
Due Sunday, February 2, before midnight
Goals
The goals for this lab assignment are:
-
Practice using a text editor such as Visual Studio Code.
-
Write your first python programs!
-
Understand and debug python error messages
-
Use variable assignment to store values
-
Get comfortable with
print()
andinput()
-
Get comfortable with python data types:
int
,float
,str
-
Use type casting with
input()
to get numeric data
1. Warmup: A first program
The file intro.py
is initially empty. Practice using an editor and type in the program below.
Since the point is to start getting used to typing Python code, you should actually type this, rather than using copy/paste. |
This part will not be graded. It is an optional (but recommended) warmup to help make sure you understand the workflow before you start trying to come up with your own code.
-
After saving your file, run the program and fix any errors.
Here is the program to type:
"""
This is a sample python program
Author: <Put your name here>
Date: <Put today's date here>
"""
#define the main function
def main():
name = input("What is your name? ")
print("Hello")
print(name)
print("Nice to meet you!")
# run the body of the main function defined above
main()
1.1. Sample output
Here are two examples of the running program. User input is shown in bold.
$ python3 intro.py What is your name? Brit Hello Brit Nice to meet you!
$ python3 intro.py What is your name? Grace Hopper Hello Grace Hopper Nice to meet you!
1.2. A note on printing strings
Printing Strings in Python
Here’s an example of printing two words separated by a single blank space:
The extra space after Hi there! You can also use
will result in the output Hi there! Welcome to CS21! |
2. Favorite Animal and Color
Write a program called animal.py
that asks the user for their favorite animal and their favorite color. Your program should then print a short response as shown below.
2.1. Sample output
Two examples of the running program are shown below. User input is shown in bold.
$ python3 animal.py
What is your favorite animal? dog
What is your favorite color? blue
Maybe you'll have a chance to draw a blue dog later in this course!
$ python3 animal.py
What is your favorite animal? duckbilled platypus
What is your favorite color? purple
Maybe you'll have a chance to draw a purple duckbilled platypus later in this course!
2.2. Requirements
Your program should meet the following requirements:
-
Ask the user for their favorite animal and favorite color, in that order.
-
Print a short response that includes their favorite animal and color.
Your output should match the examples shown above when given the same inputs.
Your solution should be contained within a main
function that you call at the end of your program
3. Python Debugging
Often a program won’t work as intended the first time we try to run it. We must review and revise the program in a process called debugging to fix syntax or logic errors in our program. The provided program fixme.py
should complete the following steps:
-
Prompt the user for an integer
n
-
Compute the value of
n
squared, i.e.n
timesn
, and store it in the variablesquare
-
Print the value and its square
However when we run the program, we get the following output:
$ python3 fixme.py
File "fixme.py", line 23
print(n + " squared is + square)
^
SyntaxError: unterminated string literal (detected at line 23)
Look closely at the program and the error output and try to fix this first error. Looking at a working example program may help.
Unfortunately, there are more errors in this program. After fixing the first, continue your debugging process to fix the other three. At some point, python may not generate any error messages, but it also may not generate any output at all! Hint: something is missing at the end of your program. Can you add it to fix the program? Compare the bottom of your non-working program to a working example in-class program.
def main():
val = int("Enter an integer n: ")
n = int(val)
square = n * 2
print(n + " squared is + square)
3.1. Sample output
When all errors are fixed, the program should run as follows, where user input is in bold:
$ python3 fixme.py
Enter an integer n: 5
5 squared is 25
$ python3 fixme.py
Enter an integer n: -3
-3 squared is 9
3.2. Requirements
Your program should meet the following requirements:
-
Run without errors.
-
Ask the user to enter a number and display the result of that value squared.
Your output should match the examples shown above when given the same inputs.
4. Python Math Operators
To familiarize yourself with Python’s math operators, complete the program called math_demo.py
that shows the result of various math operations on integer inputs. The operators for each are shown below:
-
Multiplication (
*
) -
Division (
/
) -
Floor division (
//
) - sometimes called "Integer division" or "Whole division" -
Addition (
+
) -
Subtraction (
-
)
Complete the program in the empty math_demo.py
file provided. The
program should print the result of math operations shown above. To do
so, your program should prompt the user to type in the two numbers
num1
and num2
. Both num1
and num2
should be chosen to be small
positive integer values.
4.1. Sample Output
Two sample runs are shown below. User input is shown in bold.
$ python3 math_demo.py This program tests some python math operators. Enter the first positive integer value: 1 Enter the second positive integer value: 2 1 * 2 = 2 1 / 2 = 0.5 1 // 2 = 0 1 + 2 = 3 1 - 2 = -1
$ python3 math_demo.py This program tests some python math operators. Enter the first positive integer value: 4 Enter the second positive integer value: 3 4 * 3 = 12 4 / 3 = 1.3333333333333333 4 // 3 = 1 4 + 3 = 7 4 - 3 = 1
4.2. Requirements
Your program should meet the following requirements:
-
Ask the user for two positive integer values.
-
Show the result of combining these two integers using multiplication, division, floor division, addition, and subtraction, in that order.
Your output should match the examples shown above when given the same inputs.
Your solution should be contained within a main
function that you call at the end of your program
5. Tuition Bill Calculator
At some institutions, students' tuition bills are calculated based on the number of courses they are taking, any remaining balance from previous bills, and a "general fee" that is charged no matter how many classes they register for.
Write a program called tuition.py
that calculates a tuition bill as follows:
-
The tuition is $3456.78 per course
-
The general fee is $876.54 and is always included in the bill
-
The bill also includes any remaining balance, which may be negative
Your program should prompt the user to enter the number of courses the student is registering for (which you can assume is a positive integer) and the remaining balance (which you can assume is a floating point number), and then calculate and display the total bill based on the values above.
5.1. Sample Output
Three sample runs are shown below. User input is shown in bold.
Note: you are not required to format decimals to round to the nearest penny, though you may optionally do so as described below.
$ python3 tuition.py This program calculates the tuition bill How many courses is the student taking? 4 What is the existing balance? 1234.5 The total bill is $15938.16
$ python3 tuition.py This program calculates the tuition bill How many courses is the student taking? 6 What is the existing balance? -1000 The total bill is $20617.22
$ python3 tuition.py This program calculates the tuition bill How many courses is the student taking? 5 What is the existing balance? 333.02 The total bill is $18493.460000000003
5.2. Requirements
Your program should meet the following requirements:
-
Ask the user for the number of courses the student is taking and the existing balance.
-
Compute and display the tuition bill as specified in the description above.
Your output should match the examples shown above when given the same inputs.
Your solution should be contained within a main
function that you call at the end of your program
5.3. 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).
Notice that after rounding to two decimal places, if the hundredths place is a 0, Python won’t print that last 0. Don’t worry about it for now, though, we’ll see how to handle this later in the course! |
6. Answer the Questionnaire
After each lab, please complete the short Google Forms questionnaire. Please select the right lab number (Lab 01) from the dropdown menu on the first question.
Once you’re done with that, you should run handin21
again.
Submitting lab assignments
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, including your vscode editor, the browser and the
terminal. Then click on the logout icon ( or
) 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 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!