CS8, Spring 2017

Lab08:
Using while loops
Displaying an image


Goals for this lab

By the time you have completed this lab, you and your lab partner should be able to

Step by Step Instructions

Step 0: Get together with your assigned lab partner.

Carefully choose the first pilot. Let the person who has been the pilot least often be the pilot first this time. This lab's pilot should log in and create a lab08 directory under your cs8 folder. You will work in this account for the rest of the lab. If your partner is more than 5 minutes late, ask the TA to pair you with someone else for this week.

Step 1: Start IDLE, and open a new window for function definitions

After IDLE starts, select "File=>New Window" and add a comment at the top of this new file like usual:

# lab08.py
# Name: your name(s), today's date
# Function to get a list of strings from the user

Then save it as lab08.py inside your lab08 directory. You will return to this file soon to create two functions. But first some practice.

Step 2: Practice getting text from a user and applying a while loop

Working in the Python shell window, type the following statement, then enter some text in response to the question, and finally display the text:

>>> text = input("enter some text: ")
enter some text: here I am entering text
>>> text
'here I am entering text'

Next fill a list with text the user enters, until the user types "quit" (without the quotes). This will be accomplished using a while loop, but you must get the first text item before the loop begins (this is the initialization step):

>>>item = input("enter item: ")
enter item: Hi

Now create a new empty list and start a while loop that continues until the item equals "quit". Inside the body of the loop, append the current item to the list and get the next item:

>>> textList = [ ]
>>> while (item != "quit"):
	textList.append(item)
	item = input("enter item: ")

After you enter this code followed by a blank line, the program will repeatedly get text items from the user (you in this case). After typing quit, then display the list:

enter item: this is
enter item: lots and lots
enter item: of fun
enter item: quit
>>> textList
['Hi', 'this is', 'lots and lots', 'of fun']

Step 3: Function to get a list of text items from user

Write a function (in the lab08.py file) named getStrings to do essentially what you practiced above and then return (not print) the list. Instead of instructing the user to "enter item" for each item though, please just ask once and also let the user know to enter "quit" when done. Then just use ": " to prompt the user to enter each item. Here is an example run followed by a display of the resulting list:

>>> itemList = getStrings()
Enter items; enter "quit" to quit
: apple
: pear
: orange
: quit
>>> itemList
['apple', 'pear', 'orange']

Test your function from the Python shell window and verify it works properly. Notice that "quit" is not one of the items stored in the list.

Please do not spend so much time on this step that you won't have time for Step 4 during lab - save 15 minutes to be safe. You can come back to this step again later if necessary.

Step 4: Practice displaying an image with the cImage module

Switch roles between pilot and navigator.

First you must gain access to the text's cImage module. There is a copy in the directory named lib in the cs8c class account, so that directory must be added to the system's path list in order to import it later:

>>> import sys
>>> sys.path.append('/cs/class/cs8c/lib')

Find out right now if it worked:

>>> from cImage import *

Ask for help if you get an error message.

There is a small image of the UCSB campus in the same directory as the cImage module. Type the following to get that image and assign the name image to it:

>>> image = FileImage('/cs/class/cs8c/lib/campus.gif')

Still no error messages? Great! Now find out the size of this image (in pixels), and store its dimensions in variables named width and height:

>>> width = image.getWidth()
>>> height = image.getHeight()

Go ahead and type width to learn the width, and height to learn the height, but you will use the variables in the next step so no need to memorize the values. Now create a window in which to draw the image - make it exactly the right size by using the width and height variables:

>>> window = ImageWin("UCSB", width, height)

You should see an empty window on your desktop with the title, UCSB. Finally, tell the image object to draw itself in this window:

>>> image.draw(window)

Wait for it ...

Leave the image window open for a TA to see. Go back and finish Step 3 if necessary, and be ready to demonstrate your getStrings function to the TA too.

Step 5: Show off your work and get credit for the lab

Get your TA's attention to inspect your work, and to record your lab completion.

Don't leave early though ... see challenge problems below.

Step 5a. ONLY IF YOU RAN OUT OF TIME TO HAVE THE TA INSPECT YOUR WORK

If you must complete this assignment at CSIL, then submit it with the turnin program. You MUST have both your name and your partner's name in the file in order to receive credit. Remember that the original pilot needs to do this step, since that is whose account you have been using in Phelps 3525.

Bring up a terminal window on CSIL, and cd into the original pilot's cs8 directory, and cd again into the lab08 directory. Then type the following:

turnin Lab08@cs8c lab08.py

Respond "yes" when the program asks if you want to turn in (be sure to read the list of files you are turning in), and then wait for the message indicating success.


Evaluation and Grading

Each student must accomplish the following to earn full credit for this lab:

Optional Extra Challenge

If you finish the lab early or would like an extra challenge

a. Often you want to get a list of numbers from the user, instead of strings. The procedure is mostly the same, but realize that the input function returns a string that must be converted to a number. For example, if item = "52.5" then the float function can be used to convert it to a number as follows: float(item).

Write and test a function named getNumbers that returns a list of numbers entered by the user. For now, don't worry if the user enters a string that is not a number. Here is a sample run and display of the resulting list as well as a display of the list's sum:

>>> numList = getNumbers()
Enter items; enter "quit" to quit
: 17.5
: -1.5
: 4.0
: quit
>>> numList
[17.5, -1.5, 4.0]
>>> sum(numList)
20.0

b. Write a function to display an image that takes the name of the image file as its only parameter:

def display(imageName):

The function should import cImage (functions may not use 'from module import *' syntax). That also means you must access the FileImage and ImageWin constructors by naming the module and using the dot operator:

cImage.FileImage(imageName)
cImage.ImageWin(windowTitle, width, height)

Find the width and height of the image, and make the window size match.


Prepared by Diana Franklin and Michael Costanzo.