Difference between revisions of "CSC111 Lab 9 2011"
(→Working with Functions) |
(→Working with Functions) |
||
Line 12: | Line 12: | ||
def boxIt( string ): | def boxIt( string ): | ||
noChars = len( s ) | noChars = len( s ) | ||
− | print( " | + | print( "-" * ( 2 + noChars + 2 ) ) |
print( "| " + string + " |" ) | print( "| " + string + " |" ) | ||
− | print( " | + | print( "-" * ( 2 + noChars + 2 ) ) |
def main(): | def main(): | ||
Line 28: | Line 28: | ||
* No need to add documentation for the code in this lab (or just in spots where you think you need to add markers for yourself) | * No need to add documentation for the code in this lab (or just in spots where you think you need to add markers for yourself) | ||
+ | ===Box=== | ||
+ | * Go ahead and create the boxIt() function above. | ||
+ | * Test it. Notice that I tested it with 3 different strings, one of them the empty string. It is important to test for "strange" conditions. An empty string is a '''totally valid''' string, and if our program is supposed to work with strings, it should work with empty strings as well without ''crashing''. | ||
+ | |||
+ | ===Triple Boxes=== | ||
+ | |||
+ | * Add a new function called '''BoxIt3( s1, s2, s3)''', that receives 3 strings and prints each string in its own box. | ||
+ | * If you call your function in main() like this '''BoxIt3( "Hello", "There", "Smith College" ), it will print | ||
+ | |||
+ | --------- | ||
+ | | Hello | | ||
+ | --------- | ||
+ | |||
+ | --------- | ||
+ | | There | | ||
+ | --------- | ||
+ | |||
+ | ----------------- | ||
+ | | Smith College | | ||
+ | ----------------- | ||
+ | |||
+ | * Go ahead and test your new function. Test it with different strings. | ||
+ | * If your solution does not use the function BoxIt() created earlier, modify '''BoxIt3()''' so that it calls '''BoxIt()''' 3 times. | ||
+ | |||
+ | ===Multiple Boxes=== | ||
<br /><br /><br /> | <br /><br /><br /> | ||
[[Category:CSC111]][[Category:Python]][[Category:Labs]] | [[Category:CSC111]][[Category:Python]][[Category:Labs]] |
Revision as of 12:27, 2 November 2011
--D. Thiebaut 13:15, 2 November 2011 (EDT)
Working with Functions
- For this part, work in Idle or emacs and create the various functions this lab presents you, and test them in main().
- For example, if you are asked to write a function that receives a string as parameter and prints it with a box around it, you could write something like this:
def boxIt( string ):
noChars = len( s )
print( "-" * ( 2 + noChars + 2 ) )
print( "| " + string + " |" )
print( "-" * ( 2 + noChars + 2 ) )
def main():
boxIt( "hello!" )
boxIt( "This is a very long string!" )
boxIt( "" ) # empty string
main()
- No need to add documentation for the code in this lab (or just in spots where you think you need to add markers for yourself)
Box
- Go ahead and create the boxIt() function above.
- Test it. Notice that I tested it with 3 different strings, one of them the empty string. It is important to test for "strange" conditions. An empty string is a totally valid string, and if our program is supposed to work with strings, it should work with empty strings as well without crashing.
Triple Boxes
- Add a new function called BoxIt3( s1, s2, s3), that receives 3 strings and prints each string in its own box.
- If you call your function in main() like this BoxIt3( "Hello", "There", "Smith College" ), it will print
--------- | Hello | --------- --------- | There | --------- ----------------- | Smith College | -----------------
- Go ahead and test your new function. Test it with different strings.
- If your solution does not use the function BoxIt() created earlier, modify BoxIt3() so that it calls BoxIt() 3 times.
Multiple Boxes