Difference between revisions of "CSC111 Lab 5 2015b"
(→Trim DNA) |
|||
(20 intermediate revisions by 2 users not shown) | |||
Line 4: | Line 4: | ||
<br /> | <br /> | ||
+ | <center><videoflash>tDhhDKjatLI</videoflash></center> | ||
<br /> | <br /> | ||
+ | <showafterdate after="20151007 08:00" before="20151231 00:00"> | ||
<bluebox> | <bluebox> | ||
− | This lab deals with functions, passing parameters, and returning values. It is also about for you to start the Python.org documentation on the Web. The online documentation is a great help when writing programs. The Moodle submissions are due on Friday 10/ | + | This lab deals with functions, passing parameters, and returning values. It is also about for you to start the Python.org documentation on the Web. The online documentation is a great help when writing programs. The Moodle submissions are due on Friday 10/09 at 11:55 p.m. |
</bluebox> | </bluebox> | ||
<br /> | <br /> | ||
Line 61: | Line 63: | ||
[[Image:TellerFigure.jpg|right]] | [[Image:TellerFigure.jpg|right]] | ||
<br /> | <br /> | ||
− | * Without looking at your notes, (but if you get stuck, it's ok to look up your notes or the class notes on the Web), try to recreate the teller machine program | + | * Without looking at your notes, (but if you get stuck, it's ok to look up your notes or the class notes on the Web), try to recreate the teller machine program. |
− | + | :# Start with the main program, have it call a function to get the amount (you can call it '''getAmount()'''), then it calls a function (say '''divideInBills()'''), that ''receives'' the amount, breaks it into 4 numbers, and ''returns'' a list of 4 numbers to main. And finally, at the end of main(), a function (say, '''displayResults()''') is passed the list of 4 numbers and prints them in a nicely formatted output. | |
− | + | <br /> | |
− | |||
<br /> | <br /> | ||
{| style="width:100%; background:silver" | {| style="width:100%; background:silver" | ||
Line 74: | Line 75: | ||
[[Image:QuestionMark5.jpg|right|120px]] | [[Image:QuestionMark5.jpg|right|120px]] | ||
− | :* | + | :* Instead of having the function getAmount() return 4 numbers, like this: |
+ | <br /> | ||
+ | ::<source lang="python"> | ||
+ | no20s, no10s, no5s, no1s = divideInBills( amount ) | ||
+ | </source> | ||
+ | <br /> | ||
+ | :create a new function called '''breakDown( ... )''' that gets the amount and a value, and returns the number of bills of that value, and the new amount: | ||
+ | <br /> | ||
+ | ::<source lang="python"> | ||
+ | amount, no20s = breakDown( amount, 20 ) | ||
+ | amount, no10s = breakDown( amount, 10 ) | ||
+ | amount, no5s = breakDown( amount, 5 ) | ||
+ | amount, no1s = breakDown( amount, 1 ) # you might find a simpler way to write this! | ||
+ | |||
+ | </source> | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
Line 174: | Line 189: | ||
</source> | </source> | ||
<br /> | <br /> | ||
− | <!-- ================================================================ | + | <!-- ================================================================ --> |
==Functions and Stick Figure, Version 3== | ==Functions and Stick Figure, Version 3== | ||
<br /> | <br /> | ||
− | * Use the same approach to create this stick figure | + | * Use the same approach to create this stick figure of a snow man: |
<br /> | <br /> | ||
::<source lang="text"> | ::<source lang="text"> | ||
− | + | __ | |
− | + | _|==|_ | |
− | + | ('')__/ | |
− | + | >--(`^^`) | |
− | + | (`^'^'`) | |
− | + | `======' | |
+ | |||
</source> | </source> | ||
<br /> | <br /> | ||
− | * Try to organize your functions similarly to the way | + | |
+ | * Try to organize your functions similarly to the way you did in the previous version. Below is an example of a snowMan() function. Figure out the missing pieces! | ||
<br /> | <br /> | ||
::<source lang="python"> | ::<source lang="python"> | ||
− | def | + | def snowMan(): |
− | + | hat() | |
− | + | headRightArm() | |
− | + | leftArm() | |
− | + | body() | |
− | + | base() | |
− | + | </source> | |
− | |||
− | |||
− | |||
− | |||
− | </source> | ||
<br /> | <br /> | ||
− | + | <!-- ================================================================ --> | |
− | + | <br /> | |
− | =Trimming DNA= | + | ==Trimming DNA== |
<br /> | <br /> | ||
* Assume that we have a string containing the description of a DNA sequence, say "AAGACTAAAAAAGACTT." (DNA sequences are made of 4 symbols: A, C, G, and T.) Assume we know that "AGA" is a special marker in our DNA strings. So our string is marked in this way: "A<font color="magenta">AGA</font>CTAAAAA<font color="magenta">AGA</font>CTT." | * Assume that we have a string containing the description of a DNA sequence, say "AAGACTAAAAAAGACTT." (DNA sequences are made of 4 symbols: A, C, G, and T.) Assume we know that "AGA" is a special marker in our DNA strings. So our string is marked in this way: "A<font color="magenta">AGA</font>CTAAAAA<font color="magenta">AGA</font>CTT." | ||
− | * Write a function called '''trimDNA( )''' that receives the original DNA string as a parameter, and returns the string where everything on the left and right of the markers has been removed. | + | * Write a function called '''trimDNA( )''' that receives the original DNA string as a parameter, and returns the string where everything on the left and right of the markers has been removed. We assume that the string will always contain two markers. |
* Example: | * Example: | ||
Line 231: | Line 243: | ||
<br /> | <br /> | ||
+ | ==Testing== | ||
+ | <br /> | ||
+ | Make sure your function works strings of the type A<font color="magenta">AGA</font><font color="magenta">AGA</font>CTT, or <font color="magenta">AGA</font>TTTT<font color="magenta">AGA</font>. | ||
− | =Happy Birthday!= | + | <br /> |
+ | <br /> | ||
+ | ==Happy Birthday!== | ||
<br /> | <br /> | ||
* Start with these three new functions: | * Start with these three new functions: | ||
Line 280: | Line 297: | ||
| | | | ||
− | ===Challenge # | + | ===Challenge #2=== |
|} | |} | ||
[[Image:QuestionMark8.jpg|right|120px]] | [[Image:QuestionMark8.jpg|right|120px]] | ||
Line 295: | Line 312: | ||
| | | | ||
− | ===Challenge # | + | ===Challenge #3=== |
|} | |} | ||
[[Image:QuestionMark9.jpg|right|120px]] | [[Image:QuestionMark9.jpg|right|120px]] | ||
Line 304: | Line 321: | ||
<br /> | <br /> | ||
::<source lang="text"> | ::<source lang="text"> | ||
− | Please enter the names of your friends: Jorge | + | Please enter the names of your friends: Jorge KARL Dave |
Happy birthday to you! | Happy birthday to you! | ||
Line 322: | Line 339: | ||
</source> | </source> | ||
<br /> | <br /> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
==Moodle Submission== | ==Moodle Submission== | ||
<br /> | <br /> | ||
− | + | * Submit your solution program for Challenge 3 to Moodle, in Section LAB 5 PB 3. You will need to rename your program lab5_3.py. | |
<br /> | <br /> | ||
<br /> | <br /> | ||
− | + | </showafterdate> | |
− | < | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
<br /> | <br /> | ||
− | |||
− | |||
− | |||
− | == | + | == Paper and Pencil Exercises == |
− | + | Question 1: | |
− | + | Write a program that calculates the average word length in a sentence entered by the user. Make sure you format your output to only show TWO decimal places! | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | Question 2: | |
− | + | Write a program that does the following: | |
− | + | 1. Displays the provided list of students in the class: | |
− | + | students = ["Brown,Sarah", "McCharles,Charlie", "Hudson,Stanley", "Smith,Margaret", "Brown,Ben", "Yang,Debbie"] | |
− | + | 2. Prompts the user to type in a number between 0 and the length of the list of students | |
− | + | 3. Using the user input, get the appropriate student name and print out a new user account with the new format First Initial Last Name | |
− | + | ie. Sarah Brown turns in to SBrown | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | Example Input/Output | |
− | + | >>> main() | |
− | + | 1 Brown,Sarah | |
− | + | 2 McCharles,Charlie | |
− | + | 3 Hudson,Stanley | |
− | + | 4 Smith,Margaret | |
− | + | 5 Brown,Ben | |
− | + | 6 Yang,Debbie | |
− | + | Which student do you want to process? 6 | |
− | + | ||
− | + | Debbie Yang: DYang | |
− | + | >>> | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
<!-- =================================================================== --> | <!-- =================================================================== --> | ||
Line 651: | Line 383: | ||
<!-- =================================================================== --> | <!-- =================================================================== --> | ||
− | <showafterdate after=" | + | <showafterdate after="20151013 00:00" before="20151231 00:00"> |
=Solution Programs= | =Solution Programs= | ||
<br /> | <br /> | ||
::<source lang="python"> | ::<source lang="python"> | ||
− | + | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
# --------------------- stickFigure ----------------------- | # --------------------- stickFigure ----------------------- | ||
Line 895: | Line 476: | ||
rightCalf1() | rightCalf1() | ||
+ | # ---------------------- Snow Man ---------------------- | ||
+ | """ | ||
+ | __ | ||
+ | _|==|_ | ||
+ | ('')__/ | ||
+ | >--(`^^`) | ||
+ | (`^'^'`) | ||
+ | `======' | ||
+ | """ | ||
+ | def hat(): | ||
+ | print(" __" ) | ||
+ | print(" _|==|_" ) | ||
+ | def headRightArm(): | ||
+ | print( " ('')__/" ) | ||
+ | |||
+ | def leftArm(): | ||
+ | print( ">--", end="" ) | ||
+ | |||
+ | def body(): | ||
+ | print( "(`^^`)" ) | ||
+ | print(" (`^'^'`)" ) | ||
+ | |||
+ | def base(): | ||
+ | print( " `======'" ) | ||
+ | print() | ||
+ | |||
+ | def snowMan(): | ||
+ | hat() | ||
+ | headRightArm() | ||
+ | leftArm() | ||
+ | body() | ||
+ | base() | ||
+ | |||
+ | |||
+ | snowMan() | ||
+ | |||
+ | |||
+ | |||
+ | |||
# --------------------------------------------------------- | # --------------------------------------------------------- | ||
Line 912: | Line 532: | ||
− | def | + | def singHappy( ): |
# get the name from the user | # get the name from the user | ||
name = input( "Who's birthday should we celebrate? " ) | name = input( "Who's birthday should we celebrate? " ) | ||
Line 919: | Line 539: | ||
singSong( name ) | singSong( name ) | ||
− | # | + | # ----------------- Another Version --------------------- |
def happyBirthday(): | def happyBirthday(): | ||
print( "Happy birthday to you!" ) | print( "Happy birthday to you!" ) | ||
Line 932: | Line 552: | ||
happyBirthday() | happyBirthday() | ||
− | def | + | def singHappy2( ): |
# get a line of names, separated by spaces | # get a line of names, separated by spaces | ||
line = input( "? " ) | line = input( "? " ) | ||
Line 940: | Line 560: | ||
# split the line into individual names, and sing song for each | # split the line into individual names, and sing song for each | ||
− | for name in line.split( | + | for name in line.split( ): |
singSong( name ) | singSong( name ) | ||
# blank line separates the different songs | # blank line separates the different songs | ||
Line 951: | Line 571: | ||
def main(): | def main(): | ||
− | + | newStickFigure() | |
− | + | stickFigure1() | |
− | + | snowMan() | |
− | + | singHappy() | |
− | + | singHappy2() | |
− | + | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
main() | main() | ||
Line 979: | Line 584: | ||
</showafterdate> | </showafterdate> | ||
<br /> | <br /> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
<br /><br /><br /> | <br /><br /><br /> | ||
<br /> | <br /> |
Latest revision as of 16:13, 8 October 2015
--D. Thiebaut (talk) 12:17, 22 February 2015 (EST)
<showafterdate after="20151007 08:00" before="20151231 00:00">
This lab deals with functions, passing parameters, and returning values. It is also about for you to start the Python.org documentation on the Web. The online documentation is a great help when writing programs. The Moodle submissions are due on Friday 10/09 at 11:55 p.m.
Contents
- 1 Searching the Python.org Docs
- 2 Functions
- 2.1 Working with Functions in the Console
- 2.2 Teller Machine with Functions
- 2.3 Functions and Stick Figure, Version 1
- 2.4 Functions and Stick Figure, Version 2
- 2.5 Functions and Stick Figure, Version 3
- 2.6 Trimming DNA
- 2.7 Testing
- 2.8 Happy Birthday!
- 2.9 Passing the Name as a Parameter
- 2.10 Moodle Submission
- 2.11 Paper and Pencil Exercises
- 3 Solution Programs
Searching the Python.org Docs
- Open your Web browser and go to http://python.org
- Click on Documentation, then on Python 3.x Docs.
- In the Search Bar on the left, enter string methods.
- Locate the result that contains Built-In Types, and go to that page. Locate the string methods there... (Hints: 4.7.1)
- Look at all the different methods that strings offer you.
- You're ready for the lab!
Functions
Working with Functions in the Console
- This sections serves as a review for the end="..." optional parameter in print statements.
- Try the following statements in the Python console:
>>> def test(): print( "hello" ) print( "there" ) >>> test() >>> def test(): print( "hello", end="" ) print( "there" ) >>> test() >>> def test(): print( "hello", end="-" ) print( "there" ) >>> test() >>>
Notice what happens when you add the end="" construct in the print statement. It changes the way the print() function ends printing what it has to print. By default, if you do not have an end="" construct, print() brings the cursor to the next line. But this behavior can be changed. We are going to use this, in the new program below.
Teller Machine with Functions
- Without looking at your notes, (but if you get stuck, it's ok to look up your notes or the class notes on the Web), try to recreate the teller machine program.
- Start with the main program, have it call a function to get the amount (you can call it getAmount()), then it calls a function (say divideInBills()), that receives the amount, breaks it into 4 numbers, and returns a list of 4 numbers to main. And finally, at the end of main(), a function (say, displayResults()) is passed the list of 4 numbers and prints them in a nicely formatted output.
Challenge #1 |
- Instead of having the function getAmount() return 4 numbers, like this:
no20s, no10s, no5s, no1s = divideInBills( amount )
- create a new function called breakDown( ... ) that gets the amount and a value, and returns the number of bills of that value, and the new amount:
amount, no20s = breakDown( amount, 20 ) amount, no10s = breakDown( amount, 10 ) amount, no5s = breakDown( amount, 5 ) amount, no1s = breakDown( amount, 1 ) # you might find a simpler way to write this!
Functions and Stick Figure, Version 1
- This section will be cleaner if you start a new program, rather than add to an existing program.
- Add this code section to your new program:
def head(): print( " o" ) def leftArm(): print( " /", end="" ) def torso(): print( "O", end="" ) def rightArm(): print( "\\" ) def leftLeg(): print( " /", end="" ) def rightLeg(): print( " \\" ) def stickFigure(): head() leftArm() torso() rightArm() leftLeg() rightLeg() def main(): stickFigure() main()
- Take a close look at the program. Locate the end="" construct, which prevents the print from going to the next line when it's done.
- Notice also the "\\" string. Because the '\' character is used to print special characters, like '\n' (new line), or '\t' (tab), if we want to print a regular backslash character, we have to use 2 backslashes next to each other.
- Run the program. Notice how it displays a stick figure.
Functions and Stick Figure, Version 2
- Modify the stick figure program so that it looks like the code below:
def head(): print( " o" ) def leftArm(): print( " /", end="" ) def torso(): print( "O", end="" ) def rightArm(): print( "\\" ) def leftLeg(): print( " /", end="" ) def rightLeg(): print( " \\" ) def body(): leftArm() torso() rightArm() def newStickFigure(): head() body() leftLeg() rightLeg() def main(): newStickFigure() main()
- Observe how the code is different, and the new function body() is used to display the body of the stick figure (torso plus arms).
- Using the example of body(), add a new function called legs() that will call leftLeg() and rightLeg() to draw both legs. Modify newStickFigure() by removing the calls to leftLeg() and rightLeg(), and adding instead a call to legs(). Your new function should look something like this:
def newStickFigure(): head() body() legs()
Functions and Stick Figure, Version 3
- Use the same approach to create this stick figure of a snow man:
__ _|==|_ ('')__/ >--(`^^`) (`^'^'`) `======'
- Try to organize your functions similarly to the way you did in the previous version. Below is an example of a snowMan() function. Figure out the missing pieces!
def snowMan(): hat() headRightArm() leftArm() body() base()
Trimming DNA
- Assume that we have a string containing the description of a DNA sequence, say "AAGACTAAAAAAGACTT." (DNA sequences are made of 4 symbols: A, C, G, and T.) Assume we know that "AGA" is a special marker in our DNA strings. So our string is marked in this way: "AAGACTAAAAAAGACTT."
- Write a function called trimDNA( ) that receives the original DNA string as a parameter, and returns the string where everything on the left and right of the markers has been removed. We assume that the string will always contain two markers.
- Example:
def main(): DNA = "AAGACTAAAAAAGACTT" newDNA = trimDNA( DNA ) print( newDNA )
- The output would be
AGACTAAAAAAGA
Testing
Make sure your function works strings of the type AAGAAGACTT, or AGATTTTAGA.
Happy Birthday!
- Start with these three new functions:
def happyBirthday(): print( "Happy birthday to you!" ) def happyBirthdayDear( name ): print( ("Happy birthday, dear " + name +"!" ) )
- Write a third function called singSong() that calls the two functions above. When calling happyBirthdayDear( ... ), pass it some name, say yours, or "Dave", or one of your friends. You should get this kind of output:
Happy birthday to you! Happy birthday, dear Dave!
- Modify singSong() so that it displays a more appropriate version on the song:
Happy birthday to you! Happy birthday to you! Happy birthday, dear Dave! Happy birthday to you!
Passing the Name as a Parameter
- Modify your singSong( ) function one more time. I'm highlighting the lines that should change in your function:
def singSong( name ): ... ... happyBirthdayDear( name ) ...
- Now, make your program sing "Happy Birthday" to Dave, Karl, Jorge, and Tim!
Challenge #2 |
- Make your program prompt the user for a name in the main() function.
- Then main() will pass the name to singSong(), and your program will sing Happy Birthday to the person of your choice.
Challenge #3 |
- Modify your solution program for Challenge 8 so that the user can enter several names on one line when prompted by your program. Your program then "sings" happy birthday to all the friends, one after the other...
- Example:
Please enter the names of your friends: Jorge KARL Dave Happy birthday to you! Happy birthday to you! Happy birthday, dear Jorge! Happy birthday to you! Happy birthday to you! Happy birthday to you! Happy birthday, dear Karl! Happy birthday to you! Happy birthday to you! Happy birthday to you! Happy birthday, dear Dave! Happy birthday to you!
Moodle Submission
- Submit your solution program for Challenge 3 to Moodle, in Section LAB 5 PB 3. You will need to rename your program lab5_3.py.
</showafterdate>
Paper and Pencil Exercises
Question 1: Write a program that calculates the average word length in a sentence entered by the user. Make sure you format your output to only show TWO decimal places!
Question 2: Write a program that does the following:
1. Displays the provided list of students in the class: students = ["Brown,Sarah", "McCharles,Charlie", "Hudson,Stanley", "Smith,Margaret", "Brown,Ben", "Yang,Debbie"] 2. Prompts the user to type in a number between 0 and the length of the list of students 3. Using the user input, get the appropriate student name and print out a new user account with the new format First Initial Last Name ie. Sarah Brown turns in to SBrown
Example Input/Output
>>> main() 1 Brown,Sarah 2 McCharles,Charlie 3 Hudson,Stanley 4 Smith,Margaret 5 Brown,Ben 6 Yang,Debbie Which student do you want to process? 6 Debbie Yang: DYang >>>
<showafterdate after="20151013 00:00" before="20151231 00:00">
Solution Programs
# --------------------- stickFigure ----------------------- def head(): print( " o" ) def leftArm(): print( " /", end="" ) def torso(): print( "O", end="" ) def rightArm(): print( "\\" ) def leftLeg(): print( " /", end="" ) def rightLeg(): print( " \\" ) def stickFigure(): head() leftArm() torso() rightArm() leftLeg() rightLeg() def body(): leftArm() torso() rightArm() def legs(): leftLeg() rightLeg() def newStickFigure(): head() body() legs() # --------------------- stickFigure1 ---------------------- def leftForearm1(): print( " /", end="" ) def sword1(): print( "+=====>" ) def leftArm1(): print( " \\", end="" ) def head1(): print( "O", end="" ) def rightArm1(): print( "___" ) def body1(): print( " |" ) print( " |" ) def leftThigh1(): print( " / ", end="" ) def rightThigh1(): print( "\\" ) def leftCalf1(): print( "/ ", end ="" ) def rightCalf1(): print( "\\" ) def stickFigure1(): leftForearm1() sword1() leftArm1() head1() rightArm1() body1() leftThigh1() rightThigh1() leftCalf1() rightCalf1() # ---------------------- Snow Man ---------------------- """ __ _|==|_ ('')__/ >--(`^^`) (`^'^'`) `======' """ def hat(): print(" __" ) print(" _|==|_" ) def headRightArm(): print( " ('')__/" ) def leftArm(): print( ">--", end="" ) def body(): print( "(`^^`)" ) print(" (`^'^'`)" ) def base(): print( " `======'" ) print() def snowMan(): hat() headRightArm() leftArm() body() base() snowMan() # --------------------------------------------------------- def happyBirthday(): print( "Happy birthday to you!" ) def happyBirthdayDear( name ): print( "Happy birthday, dear " + name +"!" ) def singSong( name ): happyBirthday() happyBirthday() happyBirthdayDear( name ) happyBirthday() def singHappy( ): # get the name from the user name = input( "Who's birthday should we celebrate? " ) # sing the song for that person singSong( name ) # ----------------- Another Version --------------------- def happyBirthday(): print( "Happy birthday to you!" ) def happyBirthdayDear( name ): print( "Happy birthday, dear " + name +"!" ) def singSong( name ): happyBirthday() happyBirthday() happyBirthdayDear( name ) happyBirthday() def singHappy2( ): # get a line of names, separated by spaces line = input( "? " ) # remove extra spaces at the front or back line = line.strip() # split the line into individual names, and sing song for each for name in line.split( ): singSong( name ) # blank line separates the different songs print() def bar(): print( "\n" + ( "-"*30 ) + "oOo" + ("-"*30 ) +"\n" ) def main(): newStickFigure() stickFigure1() snowMan() singHappy() singHappy2() main()
</showafterdate>