You can snarf the following files to get started:
TurtlePicture.py
draw that has no
parameters, and that can only create
turtles and call other functions.
draw .
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. Also is a statement that calls the 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 fifteen 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.
We have created two files for you to use. You should read them into your program with the urllib2. The first file has over 8000 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.
http://www.cs.duke.edu/courses/fall16/compsci101/data/earthquakeDataSept14-2016past30days.txt http://www.cs.duke.edu/courses/fall16/compsci101/data/earthquakeDataSmallFile.txt
The format of each line in the file is a "$", followed by the magnitude, followed by blank (" "), followed by a dash ("-") followed by a blank, followed by the place where the earthquake happened. For example, here is the small file.
$2.2 - 3km E of The Geysers, California $1.3 - Quarry Blast - 5km NNW of Lake Elsinore, CA $0.7 - 10km SSE of Morton, Washington $1.2 - 5km NE of San Simeon, California $1.1 - 13km SSW of Circle Hot Springs Station, Alaska $4.3 - 25km WSW of Arequipa, Peru $0.2 - 19km ESE of Anza, CA $4.1 - 8km ESE of Maltignano, Italy $0.5 - 5km S of Volcano, Hawaii $1.2 - 8km E of San Martin, California $4.3 - 5km WNW of Norcia, Italy $1.0 - 6km NNW of Garnet, CA $1.2 - 23km SW of Coalinga, California $3.5 - 5km S of Volcano, Hawaii $6.0 - 51km N of Moyobamba, Peru $3.5 - 8km ESE of Norcia, Italy $0.9 - Quarry Blast - 13km SSE of Home Gardens, CA $4.3 - 246km S of Severo-Kuril'sk, Russia $1.3 - 114km NW of Talkeetna, Alaska $1.9 - 47km W of Anchorage, Alaska $4.7 - 14km NW of San Isidro, Costa Rica $3.9 - 12km NW of Faro, Canada $0.8 - 51km ENE of Cantwell, Alaska $0.8 - 2km S of Castle Rock, Washington $4.5 - 277km NW of Visokoi Island, South Georgia and the South Sandwich Islands $4.3 - Southwest Indian Ridge $1.5 - 12km WNW of Luxora, Arkansas $5.2 - 269km NW of Visokoi Island, South Georgia and the South Sandwich Islands $0.3 - 10km W of Salcha, Alaska $-0.2 - 7km ESE of Mammoth Lakes, California $4.6 - 8km W of Amatrice, Italy $4.8 - 194km ESE of Nikol'skoye, Russia $5.0 - 264km ESE of Grytviken, South Georgia and the South Sandwich Islands $4.2 - 186km ESE of Nikol'skoye, Russia $0.5 - 12km SE of Mammoth Lakes, California $4.4 - 66km SW of Severo-Kuril'sk, Russia $1.3 - 39km W of Willow, Alaska $4.5 - 27km NNW of `Alaqahdari-ye Kiran wa Munjan, Afghanistan
The first line above shows that there was an earthquake of magnitude 2.2 that happened 3km E of The Geysers in California.
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 and the name of the place.
To get full credit you must follow these requirements.
QuakeQueries.py. You can snarf this file to get
started.
NOTE: You will need to modify the fileToList function that is provided in the snarf file to format strings in this new format.
For the example above, that list would look like (just the first three items in the list are shown):
["2.2 : 3km E of The Geysers, California", "1.3 : Quarry Blast - 5km NNW of Lake Elsinore, CA", "0.7 : 10km SSE of Morton, Washington", ... ]
if __name__ == '__main__':
'''
Starting part of the earthquake program
Read a file of earthquake data and answer
several queries about the data.
'''
urlstart = "http://www.cs.duke.edu/courses/compsci101/fall16/data/"
#datafile = "earthquakeDataSept14-2016past30days.txt"
datafile = "earthquakeDataSmallFile.txt"
eqList = fileToList(urlstart+datafile)
print "Number of lines in the file is:", len(eqList)
print
print "First six lines in the list:"
for num in range(6):
print eqList[num]
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.
Your output has to be clearly identified. Using the small datafile shown above, here is an example of how the output might be shown:
Number of lines in the file is: 38 First six lines in the list: 2.2 : 3km E of The Geysers, California 1.3 : Quarry Blast - 5km NNW of Lake Elsinore, CA 0.7 : 10km SSE of Morton, Washington 1.2 : 5km NE of San Simeon, California 1.1 : 13km SSW of Circle Hot Springs Station, Alaska 4.3 : 25km WSW of Arequipa, Peru First ten earthquakes in Peru are: 4.3 : 25km WSW of Arequipa, Peru 6.0 : 51km N of Moyobamba, Peru Number of earthquakes in Italy is 4 Number of CA quakes 2.0 or higher is: 0 Number of California quakes 2.0 or higher is: 1 Number of quakes 2.0 or higher in California total is: 1 First six earthquakes in Italy greater than 4.0 4.1 : 8km ESE of Maltignano, Italy 4.3 : 5km WNW of Norcia, Italy 4.6 : 8km W of Amatrice, Italy Largest quake is: 6.0 : 51km N of Moyobamba, Peru Largest quake in Russia is: 4.8 : 194km ESE of Nikol'skoye, Russia First Ten quakes from Alaska that are 1.0 or higher 1.1 : 13km SSW of Circle Hot Springs Station, Alaska 1.3 : 114km NW of Talkeetna, Alaska 1.9 : 47km W of Anchorage, Alaska 1.3 : 39km W of Willow, Alaska Ten random quakes from Alaska that are 1.0 or higher 1.3 : 114km NW of Talkeetna, Alaska 1.1 : 13km SSW of Circle Hot Springs Station, Alaska 1.3 : 39km W of Willow, Alaska 1.9 : 47km W of Anchorage, Alaska done processing
NOTE: Only the total number of earthquakes from CA and California is required but I chose to include additional information for the separate counts first to make it easier to determine if I was computing the total correctly.
Consider other testing. For example, you could run your program on big
quakes in California (CA or California) with quakes 1.0 or bigger and that
output should be:
If you do this be sure to change it back to 2.0 before you turn it in and
run your output.
Number of CA quakes 1.0 or higher is: 2
Number of California quakes 1.0 or higher is: 4
Number of quakes 1.0 or higher in California total is: 6
You'll need to extract/slice the magnitude from the earthquake string and convert it 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]
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.