You will need to snarf files for this lab. Those files are also here.
To get credit for lab 2, you will need to do the following by Sunday night:
APTs are run on the APT page here.
Create a Python module named Gravity (the Capital "G" is very important!). Note that when you create it, Eclipse will automatically add the .py extension to name the file Gravity.py. Also when creating it, you will see this window appear:
Select the line "Module: Main" and click OK.
Your file should then look similar to this:
The first four lines are a comment that puts the current date and tries to guess what your name is, using your username. The line
if __name__ == '__main__':
is a way for you to indicate where you want your program to start executing Python code. The word "pass" doesn't do anything. You would replace it with a line of python code that would need to be indented four spaces as "pass" is.
Now create a function named falling with the appropriate parameters (two). Strongly suggest that you cut and paste the def line under the specification of the APT definition for Gravity.
Put this function between the date/author comment and the if statement.
Answer the following questions on the online form.
Note that this is not the correct code. This is just to verify that you have named the file and the def statement correctly.
All code in the body of a function must be indented, this line needs to be indented four spaces.
Your code should look similar to this:
Run the APT by loading it into the web page, selecting the button by Gravity and then clicking on the test/run button.
How many test cases does this pass? (how many greens do you get?).
For example if the velocity is 3.0 and the time is 2.0 then that line might print:
velocity is 3.0, time is 2.0, velocity*time is 6.0
Run the APT with the APT Tester. When you run this, explain where this line of output is printed on the web page.
When you are ready to test your function, you will test it in Eclipse first with two sample inputs.
Below the "if" statement and indented, add two print statements. That is, replace the word "pass" (on line 11 above) with two lines to call and print the falling method twice, once with time set to 3.0 and velocity set to 5.0, and once with time set to 3.0 and velocity set to 0.0.
Run your program in Eclipse. What are the two answers? They should match the answers on the examples below (this picture is from the bottom of the page describing the Gravity problem).
Put the two answers you get in the form.
Put that answer in your form
Even though you are working on this APT in lab, you will still have to turn it in on the APT page and fill out the REFLECT that goes with it. Also make sure your name is on it with a comment.
In the REFLECT you will note that it was solved as part of lab 2 and who you solved it with.
Consider the following common children's song:
Here we go 'round the mulberry bush, The mulberry bush, the mulberry bush. Here we go 'round the mulberry bush, So early in the morning. This is the way we wash our clothes, We wash our clothes, we wash our clothes. This is the way we wash our clothes, So early Monday morning. This is the way we iron our clothes, We iron our clothes, we iron our clothes. This is the way we iron our clothes, So early Tuesday morning. This is the way we scrub the floor, We scrub the floor, we scrub the floor. This is the way we scrub the floor, So early Wednesday morning. This is the way we mend our clothes, We mend our clothes, we mend our clothes. This is the way we mend our clothes, So early Thursday morning. This is the way we sweep the house, We sweep the house, we sweep the house. This is the way we sweep the house, So early Friday morning. This is the way we bake our bread, We bake our bread, we bake our bread. This is the way we bake our bread, So early Saturday morning. This is the way we go to church, We go to church, we go to church. This is the way we go to church, So early Sunday morning.
This song was originally posted here .
Write a Python module/program to print out the lyrics to this song.
Do not use more advanced features than what we have covered in class (use print statements and functions).
Each of you should be writing your own code, but discussing how you are doing it and help each other debug their code.
It is easy, of course, to write the program using a series of print statements for each line of the song. But the goal of this lab is to take advantage of the cumulative structure of the song to avoid redundancy. Look for ways to use functions to avoid simple redundancy and functions with parameters to avoid the structural redundancy within the songs. In particular, make sure that you use no more than one print statement for each distinct line of the song. Your output of the song lyrics should be as similar as possible to what appears above, including line breaks and blank lines. Although if you cannot easily match the capitalization or punctuation, that is okay.
To start, use Eclipse and snarf the sample file for lab today called Farmer.py. Run this file to see what it does. Then create a new file named Mulberry.py for this program.
Answer the following questions. For the last question, the person filling out the form would cut and paste their code to give an example of what the group came up with.
(If you don't finish Part 3 during lab, you can submit just the part you have completed).
When you snarf lab2, you'll find the Python module Multiprint.py, the code is shown here:
word = "wonderful" count = 1 def printit(): global count,word print count,"\t",word count = count + 1 def f1(): printit() printit() def verse(): f1() f1() if __name__ == "__main__": verse()
When this code is run, the output is:
1 wonderful 2 wonderful 3 wonderful 4 wonderful
If you add one more call of printit() in the body of f1() and one more call of f1() in the body of verse(), so that there are three identical lines in f1() and three identical lines in verse() the output changes to:
1 wonderful 2 wonderful 3 wonderful 4 wonderful 5 wonderful 6 wonderful 7 wonderful 8 wonderful 9 wonderfulYou'll do two things with Multiprint.py. Answer these questions as you go along.
def f1(): f2() f2() def f2(): printit() printit() def verse(): f1() f1()
When you run this program there are 8 lines printed. Your goal is to modify the program so that it prints 256 lines.
You can add more functions, rename functions, but the body of each function other than printit() must consist of the same function call repeated -- as shown above, for example where each line is repeated twice in the functions shown.
With these modifications, how many total functions does your program have?