Difference between revisions of "CSC111 Lab 8 2015"
Line 13: | Line 13: | ||
# put your code here | # put your code here | ||
# | # | ||
+ | return 10 # just to make sure the code is accepted by Idle | ||
Line 51: | Line 52: | ||
# put your code here | # put your code here | ||
# | # | ||
+ | return "Y" # just to make sure the code works as is. | ||
def main(): | def main(): | ||
Line 78: | Line 80: | ||
Me too! | Me too! | ||
</source> | </source> | ||
+ | <br /> | ||
+ | ==Exercise 3: While Loop and Reading File== | ||
+ | <br /> | ||
+ | ::<source lang="python"> | ||
+ | def createFile( fileName ): | ||
+ | file = open( fileName, "w" ) | ||
+ | file.write(""" | ||
+ | Haikus from http://www.toyomasu.com/haiku/ | ||
+ | An old pond! | ||
+ | A frog jumps in- | ||
+ | The sound of water. | ||
+ | <<<END>>> | ||
+ | Feel free to use anything from this page as | ||
+ | long as you make references and links to HAIKU | ||
+ | for PEOPLE. Last updated: Jan 10th. 2001. | ||
+ | Edited by Kei Grieg Toyomasu, | ||
+ | kei at toyomasu dot com""" ) | ||
+ | file.close() | ||
+ | |||
+ | def readFileTillMarker( fileName ): | ||
+ | # | ||
+ | # add your code here | ||
+ | # | ||
+ | return [] # just to make sure the code compiles. | ||
+ | |||
+ | def main(): | ||
+ | # Part 1, Exercise 3 | ||
+ | # create a sample text file for testing. The file will contain the string <<<END>>> | ||
+ | # in it. | ||
+ | createFile( "lab8Demo.txt" ) | ||
+ | |||
+ | # get all the lines before but not including <<<END>>> | ||
+ | lines = readFileTillMarker( "lab8Demo.txt" ) | ||
+ | |||
+ | # display the lines returned. We strip them to remove the extra \n at the end and | ||
+ | # the extra spaces at the front of each line. | ||
+ | print( "Lines found by readFileTillMarker(): " ) | ||
+ | for line in lines: | ||
+ | print( line.strip() ) | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | Same idea as the previous 2 exercises. This time you need to add code to the function '''readFileTillMarker()''' so that it will read all the lines contained in the file whose name is passed as a parameter, and that appear before the marker string '''<<<END>>>'''. In other words, the function reads the lines, adds them to a list of lines, until it finds a line containing the string '''<<<END>>>'''. It returns a list of all the lines read. | ||
+ | |||
+ | The function '''createFile()''' creates a simple file you can use for testing. Take a look at it. R | ||
</showafterdate> | </showafterdate> |
Revision as of 13:46, 22 March 2015
--D. Thiebaut (talk) 14:10, 22 March 2015 (EDT)
<showafterdate after="20150325" before="20150601">
Contents
Part 1: While Loops
Exercise 1: Robust Input with While
def getPositiveNumber(): # # put your code here # return 10 # just to make sure the code is accepted by Idle def main(): # Part 1, Exercise 1 x = getPositiveNumber() print( "getPositiveNumber() returned", x ) main()
Complete the function getPositiveNumber() so that it asks the user for a number larger than 0, and keeps on prompting the user as long as she doesn't enter a valid number.
Below is an example of the interaction between computer and user.
Please enter a number greater than 0: -1 -1 is invalid. Please enter a number greater than 0: -3 -3 is invalid. Please enter a number greater than 0: 0 0 is invalid. Please enter a number greater than 0: 20 getPositiveNumber() returned 20 >>>
Exercise 2: Ask for Yes/No answer
Same idea as in Exercise 1, but this time the new function, called getYesNo() will keep on asking a question until the user's input is "YES", "yes", "y", "NO", "no", or "n". Any combination of lower/upper case in the spelling is accepted ("yES" is valid).
- Source
def getYesNo(): # # put your code here # return "Y" # just to make sure the code works as is. def main(): # Part1, Exercise 2 likesChoco = getYesNo() if likesChoco in ["YES", "Y" ]: print( "Me too!" ) else: print( "Sorry to hear that!" ) main()
- Example output
Do you like chocolate (Y/N)? possibly please enter by 'Yes' or 'No' Do you like chocolate (Y/N)? I don't know please enter by 'Yes' or 'No' Do you like chocolate (Y/N)? Oui please enter by 'Yes' or 'No' Do you like chocolate (Y/N)? y Me too!
Exercise 3: While Loop and Reading File
def createFile( fileName ): file = open( fileName, "w" ) file.write(""" Haikus from http://www.toyomasu.com/haiku/ An old pond! A frog jumps in- The sound of water. <<<END>>> Feel free to use anything from this page as long as you make references and links to HAIKU for PEOPLE. Last updated: Jan 10th. 2001. Edited by Kei Grieg Toyomasu, kei at toyomasu dot com""" ) file.close() def readFileTillMarker( fileName ): # # add your code here # return [] # just to make sure the code compiles. def main(): # Part 1, Exercise 3 # create a sample text file for testing. The file will contain the string <<<END>>> # in it. createFile( "lab8Demo.txt" ) # get all the lines before but not including <<<END>>> lines = readFileTillMarker( "lab8Demo.txt" ) # display the lines returned. We strip them to remove the extra \n at the end and # the extra spaces at the front of each line. print( "Lines found by readFileTillMarker(): " ) for line in lines: print( line.strip() )
Same idea as the previous 2 exercises. This time you need to add code to the function readFileTillMarker() so that it will read all the lines contained in the file whose name is passed as a parameter, and that appear before the marker string <<<END>>>. In other words, the function reads the lines, adds them to a list of lines, until it finds a line containing the string <<<END>>>. It returns a list of all the lines read.
The function createFile() creates a simple file you can use for testing. Take a look at it. R </showafterdate>