Hello, CS 111! lab
Welcome to CS 111! This is a lab activity, not an assignment. Therefore, it's not required that you complete it or hand it in. However, the concepts in this lab will be useful when you go to complete the first homework assignment (and will be useful for exams down the road), so I'd encourage you to put in a serious effort on it in class, and to consider finishing any remaining parts outside of class.
Exercise 0
We just did this part together, but here is a reminder:
This link has screenshots that correspond to these instructions.
- Mount the COURSES drive by double clicking the courses Desktop icon (if you didn't use the checkbox when you logged in)
- Create a new folder in your STUWORK folder called
Lab0 - Open VSCode
- Click and drag your
Lab0folder to the VSCode window to open it - Create a new file named
hello.py - In the menu under Terminal, click New Terminal to open a terminal window.
Exercise 1: Hello, CS111!
For this exercise, predict the output and then run each of the following commands in VSCode.
(Copy and paste each part of the code into your hello.py file. Then, save the file. In the
terminal window, type python3 hello.py to run the program you've created.)
# 1a print("Hello, CS111!") ## Prints: FILL IN HERE # 1b print("Hello", "CS111!") ## Prints: FILL IN HERE # 1c print("Hello") print() print("CS111!") ## Prints: FILL IN HERE
Exercise 2: Getting user input
In this exercise, you'll practice using the input() function to ask the user for input.
Part a: getting multiple inputs
We can use the input function to ask the user for multiple inputs.
print("Please type 4 numbers (ints or floats), hitting return/enter after each.") num1 = input() num2 = input() num3 = input() num4 = input() print("Their product is:", float(num1) * float(num2) * float(num3) * float(num4)) ## This program does the following: FILL IN HERE
What do you think will happen when this program runs? Predict the result and then type it up in VSCode to find out.
Part b: including a parameter to input
We can include a parameter to input to prompt the user. Type the following
lines of code in VSCode and observe the output.
# 2b: Get user input animal = input("What is your favorite type of animal? ") print("Your favorite type of animal is:", animal) ## This program does the following: FILL IN HERE
Exercise 3: Writing a function
We can write functions to make blocks of code modular (that is, to be able to call them repeatedly without writing the same lines of code in multiple places).
Predict the output of the following code, then copy it into VSCode to see what happens.
def congratulate(firstName): print("Congratulations,", firstName, "!") congratulate("Sirius") # one of my cats :) congratulate("Stella") # my other cat congratulate(Anna) # what's different here? ## This program does the following: FILL IN HERE
Extra time?
Here are some additional exercises to try out.
Punctuation
How would you add punctuation after the name, without a space in between?
Passing user input to a function
Modify the previous block of code to get the user's name with the input function and then
pass the provided input to the congratulate function.
Anna's acknowledgements
This lab was originally created by Tanya Amert.