Beruflich Dokumente
Kultur Dokumente
In the real world, how many times have you used something
like a console to enter data?
Probably zero.
Pros and Cons
Consoles have a few problems
1. You cannot enter lots of data easily.
2. They are ugly.
3. You can only do string input.
4. You can only do one thing at a time.
5. If you make a mistake entering data you are screwed.
On the other hand,
1. Consoles are easy to program
2. If you are the only user of the program, theyre fine
3. If a program is very small, theyre fine.
Other types of input/output
There are four different types of Input/Output that well talk
about this semester.
Files
Internet
Drawing (Output only)
Graphical User Interface (GUI)
Files
There are many many different types of files for storing data.
In this class, we focus on comma separated values (CSV) files
CSV files are great for holding data associated with a 2-
dimensional array.
Each line of the file corresponds to row of the array.
In each line, each value is separated by a comma
hence the name
If you store strings, then each string is put in quotes
row/col 0 1 2
0 20 200 70
CSV Example 1
2
30
40
300
400
80
90
3 50 500 100
4 60 600 110
20,200,70
30,300,80
40,400,90
50,500,100
60,600,110
data.csv
row/col 0 1
0 hello C Patterson
CSV Example 1
2
guy, gal
fruit
tree
a b c d
The variable is file handle that will let us get a the contents
of the file
The "r" means open for reading
The filename needs to be the exact file name we are using
Close a file
Once you are done with a file, you should close it.
This is true if you read or write a file
This is done via the close command.
aLine = "20,30,40,50"
aList = aLine.split(",")
sum = 0
for element in aList:
intValue = int(element)
sum+=intValue
print(sum)
myTurtle.penup()
myTurtle.setpos(20,30)
myTurtle.dot(10)
myTurtle.setpos(0,0)
myTurtle.pendown()
myTurtle.color("red")
myTurtle.right(90)
myTurtle.fd(50)
myTurtle.right(90)
myTurtle.fd(50)
myTurtle.right(90)
myTurtle.fd(50)
myTurtle.right(90)
myTurtle.fd(50)
turtle.exitonclick()
speed
Turtle is very slow
import turtle
We can make it faster using the myTurtle = turtle.Turtle()
method speed() myTurtle.color('red', 'yellow')
myTurtle.speed(0)
Speed takes a number between myTurtle.begin_fill()
0-9 myTurtle.forward(200)
1 is the slowest 9 is very fast. 0 is myTurtle.left(170)
while abs(myTurtle.pos())>=1:
the fastest myTurtle.forward(200)
There is no good reason for this. myTurtle.left(170)
Someone just felt they were clever myTurtle.end_fill()
when they created turtle
turtle.exitonclick()
Just use 0.
World Coordinates
We can also change the world coordinates
myTurtle = turtle.Turtle()
minX = -50
minY = -50
maxX = 100
maxY = 100
screen = myTurtle.getscreen()
screen.reset()
screen.setworldcoordinates(minX, minY, maxX, maxY)
Examples
Write a method with one parameter: sideLength. The
function should draw a square with side length sideLength.
Modify your function so that it has two parameters:
sideLength and numSides. The function should draw a
regular polygon with numSides sides and side length
sideLength. Hint: The sum of the angles in a regular
polygon is 360 degrees.
Internet
Setup
The internet is just a big file system
The Uniform Resource Locator (URL) tells us which computer
contains the file we want, the directory of the file, and specific
file in that directory.
http://daringfireball.net/linked/2016/06/20/leventhal-apfs/index.html
There is a bit more to it than this, but thats fine for now
Opening URL
How we interpret a file depends on its type.
For example .jpg is an image .txt is a text file
For right now we are only going to work text files, which are
called ascii files.
After we decode it, then it becomes similar to a file.
Simple Request
import urllib.request
page = urllib.request.urlopen("http://ichart.finance.yahoo.com/table.csv?s=AAPL")
pageBytes = page.read()
pageText = pageBytes.decode("ascii")
pageList = pageText.splitlines()
data = []
for line in pageList[1:]:
data += [line.split(",")]
print(data)
page.close()
import urllib.request
import turtle
page = urllib.request.urlopen("http://ichart.finance.yahoo.com/table.csv?s=FB")
pageBytes = page.read()
pageText = pageBytes.decode("ascii")
pageList = pageText.splitlines()
data = []
for line in pageList[1:]:
data += [line.split(",")]
maxX = len(data)
maxY = 0
for line in data:
if maxY < int(float(line[4])):
maxY = int(float(line[4]))
myTurtle = turtle.Turtle()
myTurtle.speed(0)
screen = myTurtle.getscreen()
screen.reset()
screen.setworldcoordinates(0, 0, maxX, maxY)
myTurtle.pu()
x = maxX
for line in data:
myTurtle.setpos(x, int(float(line[4])))
print(line[0])
myTurtle.dot(3, "red")
x -= 1
page.close()
turtle.exitonclick()
Examples
Write a function called stockPlot2 with two parameters:
stock1 and stock2. These two parameters should be the
stock symbols for two companies. This function should plot
the closing stock prices for these two companies in different
colors.
Write a function called stockPlot3 with two parameters:
stock and dataType. The first parameter is the stock
symbol, and the second parameter indicated what kind of
data we want to plot (opening price, high, low, or closing
price). This function should plot the indicated data for the
company.
Examples
Write a function called stockPlotDate with three
parameters: symbol, startDate, and endDate. It should
plot the closing stock prices for the indicated company from
the startDate to the endDate. Hint: See if you can figure
out what this website displays:
http://ichart.finance.yahoo.com/table.csv?s=AAPL&a=9&b=2
3&c=2007&d=0&e=5&f=2015
Write a function called stockPlotAll with one parameter:
symbol. This function should plot the high, low, open, and
close data for the indicated company.