CS21 Lab 1: First Programs

Due Saturday, September 16, 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!

  • Use variable assignment to store values

  • Get comfortable with print() and input()

  • Get comfortable with python data types: int, float, str

  • Use type casting with input() to get numeric data

1. A first program (Optional Warmup)

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()

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? Shafi Goldwasser
Hello
Shafi Goldwasser
Nice to meet you!

A note on printing strings

Printing Strings in Python

Here’s an example of printing two words separated by a single blank space:

print("Hi " + "there!")

The extra space after Hi and BEFORE the quotation will result in the following output:

Hi there!

You can also use print() with no arguments to print a blank line, e.g.:

print("Hi " + "there!")
print()
print("Welcome to CS21!")

will result in the output

Hi there!

Welcome to CS21!

2. Favorite Animal

Write a program called animal.py that asks the user for their name and favorite animal (plural) and prints a short response as shown below.

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

$ python3 animal.py
What's your name? Andrew
Hello, Andrew!
What is your favorite animal? Puppies
Amazing! I like Puppies too.


$ python3 animal.py
What's your name? Markus
Hello, Markus!
What is your favorite animal? Ruby-throated hummingbirds
Amazing! I like Ruby-throated hummingbirds too.

You do not need to worry if the user types in a non-plural animal like turtle.

3. Python Math Operators

To familiarize yourself with Python’s math operators, complete the program called math_demo.py that shows the result of addition, multiplication and exponentiation. The operators for each are shown below:

  • Addition (+)

  • Multiplication (*)

  • Division (/)

  • Integer division (//)

  • Exponentiation (**)

  • Mod (%)

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.

Two sample runs are shown below. User input is shown in bold.

3.1. Sample Output

$ python3 math_demo.py

This program tests some python math operators.
Enter the first positive integer value: 10
Enter the second positive integer value: 6

10 + 6 = 16
10 * 6 = 60
10 / 6 = 1.6666666666666667
10 // 6 = 1
10 ** 6 = 1000000
10 % 6 = 4
$ python3 math_demo.py

This program tests some python math operators.
Enter the first positive integer value: 3
Enter the second positive integer value: 5

3 + 5 = 8
3 * 5 = 15
3 / 5 = 0.6
3 // 5 = 0
3 ** 5 = 243
3 % 5 = 3

4. Tip Calculator

Not sure how much to tip your waiter? Python can help! For the last program this week, you’ll write a tip calculator program, tips.py. Given a decimal number input representing a restaurant bill, tips.py should report tip amounts for 10%, 15%, and 20%, along with the totals. Note: you are not required to format decimals to round to the nearest penny. We haven’t learned how to do that yet.

$ python3 tips.py
How much was the bill? 10.00

10% tip: $1.0, Total: $11.0
15% tip: $1.5, Total: $11.5
20% tip: $2.0, Total: $12.0
$ python3 tips.py
How much was the bill? 22.39

10% tip: $2.2390000000000003, Total: $24.629
15% tip: $3.3585, Total: $25.7485
20% tip: $4.478000000000001, Total: $26.868

4.1. 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. We haven’t seen how to fix that yet so for this lab, that is the expected output.

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.

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, like the browser and the terminal. Then click on the logout icon (logout icon or other logout icon) 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 xlock 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!