You can snarf the following files to get started:
TurtlePicture.py
if __name__ == '__main__': # main function to have a turtle draw a picture draw()
Here is example code for a function that draws a flower pattern and uses a for loop. The last line of the code is an example of calling this function.
SINCE I AM GIVING YOU THIS FUNCTION, you can use it if you want, but it does not count as one of the six functions that you are suppose to create! If you modify it in some way, (possibly add colors or fill in), then you can count it as one of your functions.
def flowerSquare(alex): # draw a flower made out of squares for i in range(12): # repeat twelve times # draw a square alex.forward(50) alex.left(90) alex.forward(50) alex.left(90) alex.forward(50) alex.left(90) alex.forward(50) alex.left(90) # move 30 degrees alex.left(30) flowerSquare(alex)
and here is the picture of the flower pattern:
Here is a Python file TurtleFlower.py that uses the flowerSquare function and draws a flower.
The textbook has already shown you several turtle methods.
For example, here are five turtle methods you have probably already used:
forward left right up down
There are many more in Python's Turtle Graphics module.
Here is the starter file TurtlePicture.py. Only two of the turtle methods below count as the required ten different turtle methods, they would be "forward" and "left".
import turtle # allows us to use the turtles library wn = turtle.Screen() # creates a graphics window def rightAngle(turt): # This function is a sample, you can delete it # sample function to draw a right angle turt.forward(100) turt.left(90) turt.forward(100) def draw(): # create a turtle and draw a right angle alex = turtle.Turtle() # you can rename the turtle if you want rightAngle(alex) if __name__ == '__main__': # main function to have a turtle draw a picture draw() wn.exitonclick() # Must be last line in file
Your grade will be based on how well your program conforms to the
standards above and whether it draws a picture using the function
named draw
.
But the main thing is to have some fun in creating your picture!
We have created two files for you to use. You should read them into your program with the urllib2. The first file has over 9000 lines of data. That's pretty big to use to debug your program. We have also created a small file with just a few lines, the second link below. We suggest you use that file initially for testing your program. You could also create your own test file.
http://www.cs.duke.edu/courses/fall17/compsci101/data/earthqDataAug14-Sep13-2017.txt # large file, 30 days of data http://www.cs.duke.edu/courses/fall17/compsci101/data/earthqDataSmallSep2017.txt # small sample file
The format of each line in the file is the magnitude, followed by a "%", followed by the type of earthquake, followed by "$", followed by the location. For example, here is the small file.
1.3%earthquake$81km SSW of Kobuk, Alaska 1.92%earthquake$37km SW of Challis, Idaho 1.5%earthquake$74km NNW of Ester, Alaska 2.3%earthquake$30km SE of Yerington, Nevada 1.45%earthquake$6km NE of Aguanga, CA 2.3%earthquake$16km SSE of Fort Irwin, CA 1.7%other event$113km NW of Redoubt Volcano, Alaska 1.95%earthquake$6km W of Volcano, Hawaii 0.03%earthquake$12km NNE of West Yellowstone, Montana 1.19%earthquake$32km WSW of Frenchtown, Montana 0.99%earthquake$36km ENE of Coso Junction, CA 1.32%earthquake$6km WNW of The Geysers, California 1.39%earthquake$14km ESE of Lincoln, Montana 1.43%earthquake$16km ENE of Ocotillo, CA 1.1%earthquake$64km ENE of Cape Yakataga, Alaska 1.85%earthquake$10km ENE of Pahala, Hawaii -0.62%earthquake$33km W of Lincoln, Montana 0.48%earthquake$20km ESE of Anza, CA 1.07%earthquake$14km ESE of Lincoln, Montana 3.2%earthquake$70km WNW of Skagway, Alaska 4.3%earthquake$47km S of San Francisco del Mar, Mexico 1.06%earthquake$5km W of Imperial, CA 5.1%earthquake$28km N of Subtanjalla, Peru 2.3%earthquake$12km SSW of Guanica, Puerto Rico 3.8%earthquake$94km NE of Chirikof Island, Alaska 2.18%earthquake$26km E of Honaunau-Napoopoo, Hawaii 4.4%earthquake$67km S of San Francisco del Mar, Mexico 4.6%earthquake$120km SW of Huarmey, Peru 4.2%earthquake$12km NNW of Juliaca, Peru 2.4%earthquake$45km SSE of Dillingham, Alaska 4.3%earthquake$South of the Fiji Islands 1.02%earthquake$8km E of Tijuana, B.C., MX 2.9%earthquake$3km NE of Barahona, Puerto Rico 4%earthquake$82km SSW of Paredon, Mexico 1.09%earthquake$5km NNW of The Geysers, California 2.5%earthquake$13km ESE of Soda Springs, Idaho 0.55%earthquake$9km NE of Aguanga, CA 4.5%earthquake$41km SW of Pijijiapan, Mexico 4.4%earthquake$17km NNE of Jayune, Peru 2.59%earthquake$22km N of Luquillo, Puerto Rico 4.2%earthquake$246km ESE of Chirikof Island, Alaska 2.65%earthquake$71km N of Tierras Nuevas Poniente, Puerto Rico 1.7%earthquake$33km E of Sutton-Alpine, Alaska 5.2%earthquake$103km SSW of Paredon, Mexico 1.38%earthquake$14km SE of Lincoln, Montana 3.05%earthquake$70km N of Tierras Nuevas Poniente, Puerto Rico 1.3%earthquake$68km WNW of Valdez, Alaska 4.1%earthquake$80km SW of Paredon, Mexico 3.05%earthquake$70km N of Tierras Nuevas Poniente, Puerto Rico 4.6%earthquake$33km S of Layo, Peru
The first line above shows that there was an earthquake of magnitude 1.3 that happened 81km SSW of Kobuk, Alaska.
In our data file, we did not include all the information about each earthquake, so you will not know the longitude and latitude where it occurred, the data and time, etc.. Our data file only has the magnitude, the type or category of earthquake (such as "earthquake" or "quarry blast") and the name of the place where the earthquake happened.
Note that magnitudes can be NEGATIVE Numbers. There is an example above with -0.62 magnitude from 33km W of Lincoln, Montana.
To get full credit you must follow these requirements.
QueriesQuakes.py
. You can snarf this file to get
started.
NOTE: This function fileToList has already been started for you. You will see it when you snarf the starter files. Currently this function reads in lines of data and puts them in a list. You will need to modify this function to format strings to a different format before putting them in the list.
For the example above, that list with strings in the new format would look like (just the first three items in the list are shown):
["earthquake, 1.3, 81km SSW of Kobuk, Alaska", "earthquake, 1.9, 37km SW of Challis, Idaho", "earthquake, 1.5, 74km NNW of Ester, Alaska", ... ]
In all the cases, print the earthquakes one per line.
For example, if you call the function with 8 but there are only 4 earthquakes in the list, then just print the 4 earthquakes, one per line. If you call the function with 8 and there are 300 earthquakes in the list, then just print the first 8, one per line. Assume the function is called correctly, that is, it is not called with 0 (makes no sense) or other negative numbers.
This function returns a list of three items, 1) the magnitude as a float, 2) the type of earthquake as a string, and 3) the location of the earthquake as a string.
For example, the call getParts("earthquake, 1.3, 81km SSW of Kobuk, Alaska") would return the list
[1.3, "earthquake", "81km SSW of Kobuk, Alaska"]
You should find this function useful to call whenever you need a part of a line of earthquake data, such as you need the magnitude, or you need the type of the activity (such as earthquake).
If you write a function that returns a list of earthquake strings, then you can just call PrintQuakes to print them.
Your output should be easy to identify. Don't just print a list of earthquakes, but identify the earthquakes and then print them out. For example you might have: "Here are the first ten earthquakes in the file that happened in Puerto Rico", and then follow that with one line for each earthquake. Your program should also work correctly if we test it on a different file in the same format.
Information to print:
Your output has to be clearly identified. Using the small datafile shown above, here is an example of how the output might be shown:
Information about Earthquakes from the 30 days leading up to September 13, 2017. Number of lines in the file is: 50 Number of lines categorized as earthquakes in the file is: 49 First ten lines in the file: earthquake, 1.3, 81km SSW of Kobuk, Alaska earthquake, 1.92, 37km SW of Challis, Idaho earthquake, 1.5, 74km NNW of Ester, Alaska earthquake, 2.3, 30km SE of Yerington, Nevada earthquake, 1.45, 6km NE of Aguanga, CA earthquake, 2.3, 16km SSE of Fort Irwin, CA other event, 1.7, 113km NW of Redoubt Volcano, Alaska earthquake, 1.95, 6km W of Volcano, Hawaii earthquake, 0.03, 12km NNE of West Yellowstone, Montana earthquake, 1.19, 32km WSW of Frenchtown, Montana First ten earthquakes in Puerto Rico are: earthquake, 2.3, 12km SSW of Guanica, Puerto Rico earthquake, 2.9, 3km NE of Barahona, Puerto Rico earthquake, 2.59, 22km N of Luquillo, Puerto Rico earthquake, 2.65, 71km N of Tierras Nuevas Poniente, Puerto Rico earthquake, 3.05, 70km N of Tierras Nuevas Poniente, Puerto Rico earthquake, 3.05, 70km N of Tierras Nuevas Poniente, Puerto Rico Number of earthquakes in Mexico is 6 Number of California quakes 1.0 or higher and smaller than 3.0 is 2 First five earthquakes in Alaska 3.0 or greater are earthquake, 3.2, 70km WNW of Skagway, Alaska earthquake, 3.8, 94km NE of Chirikof Island, Alaska earthquake, 4.2, 246km ESE of Chirikof Island, Alaska Types of activity: earthquake other event Largest quake is: earthquake, 5.2, 103km SSW of Paredon, Mexico Largest quake in Hawaii is: earthquake, 2.18, 26km E of Honaunau-Napoopoo, Hawaii Ten random quakes from Peru earthquake, 4.2, 12km NNW of Juliaca, Peru earthquake, 5.1, 28km N of Subtanjalla, Peru earthquake, 4.4, 17km NNE of Jayune, Peru earthquake, 4.6, 120km SW of Huarmey, Peru earthquake, 4.6, 33km S of Layo, Peru done processing
REMEMBER, once you have this output correct you need to run your program on the large data file to turn in.
If we ask for the max and there is a tie, then any of the answers that tie can be given as a correct answer.
You'll need to convert the magnitude to a number so you can compare it to another number.
Here is sample code for reading all the lines from the nytimes.com website we did earlier in the semester.
import urllib2 f = urllib2.urlopen("http://nytimes.com") for line in f: print line
There are many ways to choose list elements at random. The simplest one is to shuffle the list, then take a slice of the shuffled list. To use the shuffle function, you must import random. See the PyDev console pasted below for details.
import random nums = [1,3,5,7,30,52] nums Out[4]: [1, 3, 5, 7, 30, 52] random.shuffle(nums) nums Out[6]: [5, 3, 52, 1, 7, 30] random.shuffle(nums) nums Out[8]: [3, 52, 5, 1, 30, 7] nums[0:3] Out[9]: [3, 52, 5]
If you want to include a picture of your turtle program, below describes how to do this. THIS IS OPTIONAL, IT IS NOT REQUIRED. It is worth 0 points.
First you could turn in your program and picture with websubmit by selecting files.
Here is how you can put your picture into your eclipse folder, and then turn it in with your programs easily.
Suppose your picture is named "image.jpg". Then in Eclipse in your assignment3 folder, create a new file and call it image.jpg
You may get a pop up that talks about "editors available on marketplace", just cancel that.
Now Eclipse knows about the file but it is empty.
Find your image file (on a regular file search or browse and copy it. Then browse to your workspace and the folder for assignment3 and copy the image file onto itself. They have to be the same name for this to work. If one is image.jpg and the other is image.JPG this won't work.
If it recognizes that it is the same file it will ask if you want to replace the old file with this one and yes you do.
Now your image looks like a file in your assignment3 folder. Don't click on it, it is too big to view and will display in a weird format. But now when you do ambient submit, you can choose it as an option.
In general you do not want to copy files into your eclipse folder unless you create a new file first with the same name and then replace it. Each time you create a file in Eclipse, then Eclipse creates additional hidden files with information on your file. If you just copy a file in, it won't have that hidden information and may not appear in Eclipse. By creating a fake file first with the same name, you get the hidden files with info about the file and you can copy in as a replace. That works.
If this is too complicated, then try websubmit which is listed on the assignment web page. There you browse and select files to submit.