CS 110X Feb 05 2014 : LAB THREE
High achievement always takes place in the framework of high expectation.
Charles Kettering
1 To conquer frustration, one must remain intensely focused on the outcome, not the obstacles
1.1 Lab Assignment
In this third lab, you will write your first stand-alone function, patterned after the example shown in lecture on February 4th.
Write a Python module with a function,
readConstrainedList(low,high, that reads, one at a time, a list of
values. If the value is greater than or equal to low and less than or
equal to high, the value is accepted, otherwise the user is warned.
When the user is done, he need only press Enter
(no input) in response to the prompt. Once all values have been entered,
the function must return the list of values that had been entered.
Write a lab3 method that invokes readConstrainedList(0,100) and
prints out the list entered by the user.
1.2 Structure
Start with the following Python module structure and create a file called lab3.py. Once you are done with this assignment, be sure to submit this file via turnin.
# Lab 3 # Author: <<Your Name>> def readConstrainedList(low, high): print ("Write code to read in the list and return it") print ("Pattern this logic after Tuesday’s lecture") def lab3(): # input print ("Add code here to invoke readConstrainedList") # output print ("Add code here to print out the list the user entered.")
Feel free to review the lecture of February 4th, but please follow this advice. Do Not Simply Copy and Paste the code from yesterday’s lecture!!! Please try to type the code from scratch because you will actually learn more in this way.
1.3 Review
Make sure your program has a well-defined # Input section, # Process section and # Output section.
Make sure your program tells the user about the constrained range [0-100] so he will be aware when he starts entering numbers.
I expect you can complete the readConstrainedList code with a while loop whose body of statements appends a value to a list if it meets the criteria (between and including 0 and 100).
1.4 Validation
You should be able to validate your code so it matches the following output.
>>> lab3() Enter values between and including 0-100. Simply press Enter when done Enter a number: 2 Enter a number: 10 Enter a number: 10 Enter a number: 3 Enter a number: -2 The value must be between and including 0-100. Enter a number: 212 The value must be between and including 0-100. Enter a number: [2, 10, 10, 3]
1.5 Prepare For Thursday
To prepare for Thursday, you simply need to type the following statements into a Python Shell window and see if you can understand or follow what is going on. Should you have trouble, re-read page 38 of the text book.
>>> xPos = 10 >>> yPos = xPos >>> xPos = 11 >>> print (yPos)
Now what do you think will happen when you print out the value of y? before you type the above print statement, see if you can work this out before printing out the value.
Can you explain why the value was printed?
Now try the more interesting scenario:
>>> myList = [1, 2, 4, 5] >>> yourList = myList >>> myList[2] = 99 >>> print (yourList)
Now what do you think will happen when you print out the value of yourList? before you type the above print statement, see if you can work this out before printing out the value.
Can you explain why the value was printed? We will explain this issue in class on either Thursday or Friday.
1.6 Prepare for Thursday: Part II
One last scenario to test (for Thursday). Type the following commands in a Python shell window.
>>> myList = [1, 2, 4, 5, 6] >>> myList[1] 2 >>> myList[3] 5 >>> myList[1:3] [2,4]
You have seen the use of the myList[i] notation to retrieve the ith value in a list. This week you will be introduced to the slice capability of Python, namely, to retrieve a "slice" or subset of a list. Note that the slice semantics follows the standard we have defined in class.
The value of myList[1:3] is the sublist starting at index position 1 and continuing up to but not include position 3. As you can see above, this means the sublist [2,4].
1.7 Prepare for Thursday: Part III
You might not find it interesting to extract a sublist of a list. But for this second part of the course, we are going to investigate string operations and manipulations. There are many correlations between lists and strings. In this Python is consistent, and much of what you have learned about lists can be immediately applied to strings.
>>> myName = "Heineman,George" # Use your own name! >>> myName[1] ’e’ # the 2nd character in the string >>> myName.index(’,’) 8 # index position of ’,’ in string >>> myName[0:8] ’Heineman’ # just my last name >>> myName[9:len(myName)] ’George’ # just my first name >>> myName[:8] ’Heineman’ # simpler way to get string prefix >>> myName[9:] ’George’ # simpler way to get string suffix
We will cover these examples on Thursday. Recreate the above in a Python shell using your own name.
You must make sure you submit your lab3.py file via turnin to receive credit for the lab.
1.8 Version : 2014/02/14
(c) 2014, George Heineman