Goals for today's lab
Today, you will learn how to write simple programs that rely upon user input and also how to use various shapes, colors, images and depths to build creative scenes like the ones shown below.What we'll cover today:
- Downloading files from the cs server (via Fetch/WinSCP)
- Creating, saving and running Python files (Canopy)
- Asking for user input and printing to Canopy's interaction pane
- Experimenting with cs1graphics (Python)
- Uploading your lab work to the cs server (via Fetch/WinSCP)
- a 40 min screencast of lab1 (snowstorm last semester)
- How to download WinSCP for PCs
- Wellesley's Fetch page (note that this link only works on campus).
Here is an older link if you are off-campus at the moment.
Set up for lab: Folders, Files and Canopy
We will connect to the CS server (cs.wellesley.edu) to download the folder that we need for today's lab. In lab, we'll use Fetch on the Macs, but there is an equivalent program called WinSCP for PCs.- Use Mac's spotlight to find Fetch and start it
- Fetch will open this window
Type in the name of the server (you can shorten it tocs
when on campus), and your username (usually first initial and first 7 letters of last name) and your password. Your password will be the one you supplied in the account request form (if you forgot your password, emailcs-sysadmin
and ask them to reset it for you). Make sure you are connecting using SFTP (indicated by the second red oval above). - If you login successfully, you will see this window:
- Double click on the cs111 folder to open it, and then on the download folder. This is where we will place programs for you to access.
- Drag a copy of the lab1_programs folder onto your desktop. Rename the folder from lab1_programs to yourname_lab1_programs. Each time you download a folder, change the name to reflect that it is yours.
OLD NEW - Look inside the folder and inspect the contents.
Let's start Canopy
Ok, time to write some python code! Let's get Canopy started (we will always use Canopy to write and run our code this semester). Use the spotlight to find Canopy (installed on all campus machines, and freely downloadable -- see Reference page).
Click on the Editor button, and then in the window that pops open, click on the "Create a new file" button. This will produce the window below. This is where you'll write your code.
- Change your working directory to be, for
today, your newly renamed
lab1_programs
folder. Click on the little arrow circled in purple and navigate to yourlab1_programs
folder. In general, you will have your relevant files in one folder on your Desktop, and you will set your working directory to be that folder. - You can type in the interactive python pane at the bottom to test things out
- You can write your own python programs in the main window. Start out with comments, indicating your name, the date, and the contents of this file.
# Oprah Winfrey
# CS111, Lab 1
# September 2, 2015
# lab1.py
- Save the file as
lab1.py
in yourlab1_programs
folder on your Desktop (either File > Save or click on the little disc icon) - You can run your file (nothing will happen just yet, but it's good to know how to run things). To run a python program, click on the green arrow, or Run > Run File or Command-R.
Part 1: Getting user input and printing in the interaction pane
In your filelab1.py
, write some Python code that will run and produce
output similar to that shown below. The yellow boxes represent Canopy's interaction pane. The user input is shown in blue bold italics.
Task 1a: Asking for a name
What is your name? Hermione
Hi Hermione!
Task 1b: Printing length of name
What is your name? Hermione
Hermione, your name contains 8 letters.
Task 1c: Asking for a favorite number
What is your favorite number? 5
Your fave number is 5!
Task 1d: Asking for both name and number and printing the name number times
The interaction pane would look like this:What is your name? Hermione What is your favorite number? 2 HermioneHermioneAnother run:
What is your name? Voldemort What is your favorite number? 6 VoldemortVoldemortVoldemortVoldemortVoldemortVoldemort
Task 1e: Ranking ice cream flavors
In this task, the user provides their name, and then ranks three flavors. Then, your program will produce a line of stars reflecting the number entered for each flavor. Below are two sample runs of our ice cream ranking program:What is your name? Ron Rate strawberry on a scale of 1-10: 10 Rate vanilla on a scale of 1-10: 2 Rate chocolate on a scale of 1-10: 5 Flavor ratings for Ron: Strawberry: ********** Vanilla: ** Chocolate: *****
What is your name? Fred Rate strawberry on a scale of 1-10: 4 Rate vanilla on a scale of 1-10: 8 Rate chocolate on a scale of 1-10: 1 Flavor ratings for Fred: Strawberry: **** Vanilla: ******** Chocolate: *
Task 1f: Ranking ice cream flavors (with cleaner output)
In this task, the printing of the flavor rankings is aligned so that all the semicolons line up vertically. This makes it easier to see how the ice cream flavors stack up against each other. The only difference in output is that the flavors would print like this:Flavor ratings for Fred: Strawberry: **** Vanilla: ******** Chocolate: *
Part 2: Creating graphic scenes with cs1graphics
Create a new file calledlab1_graphics.py
, still within your lab1_programs folder.
Some sample scenes using cs1graphics
![](harry.png)
![](beret.png)
![](frozen.png)
![](swell.png)
![](treasure.png)
![](pacman.png)
![](grace.png)
![](day.png)
![](jungle.png)
![](harry.png)