Midterm Exam 2 Review Fall 2003: Graphics from a List
Answers? Comments? Questions on Answers? Questions on Comments?
(Back to Fall2003 Midterm Review 2)
def doGraphics(list):
canvas = makePicture(getMediaPath("640x480.jpg"))
nlist = []
for entry in list:
nlist = nlist + [entry.split(" ")]
for entry in nlist:
if entry[0] == "b":
if len(entry) == 3:
setColor(getPixel(canvas,int(entry[1]),int(entry[2])),black)
else:
print "syntax error!"
elif entry[0] == "l":
if len(entry) == 5:
addLine(canvas,int(entry[1]),int(entry[2]),int(entry[3]),int(entry[4]))
else:
print "syntax error!"
else:
print "syntax error!"
show (canvas)
return canvas
houman
| Does this actually work? I'm concerned about the splitting so early. Mark Guzdial |
yeah it works.
we have no idea what elif and else means so we tried this....
def doGraphics(list):
canvas = makePicture(getMediaPath("640x480.jpg"))
list = []
for entry in list:
list = list + [entry.split(" ")]
for entry in list:
if len(entry)== 3:
setColor(getPixel(canvas,entry[1],entry[2]),black)
if len(entry) == 5:
addLine(canvas,entry[1],entry[2],entry[3],entry[4])
print "i'm glad this is taking hours"
show (canvas)
return (canvas)
however, it returns a empty canvas....please help
| What a great solution! The problem is something simple: You input "list" but then the first thing you do is set "list= []"! POOF! There goes your input! You also forgot to convert the strings back into integers. Below is my tweak to your solution. Mark Guzdial |
def doGraphics(alist):
canvas = makePicture(getMediaPath("640x480.jpg"))
list = []
for entry in alist:
list = list + [entry.split(" ")]
for entry in list:
print entry
if len(entry)== 3:
print "setting pixel"
setColor(getPixel(canvas,int(entry[1]),int(entry[2])),black)
if len(entry) == 5:
print "drawing line"
addLine(canvas,int(entry[1]),int(entry[2]),int(entry[3]),int(entry[4]))
print "i'm glad this is taking hours"
show (canvas)
return (canvas)
what does list=[] do?
It makes the variable list equal to an empty list.
what do you mean variable list equal to an empty list. Please give us example how list=[] works. thanx
i don't fully understand split could you explain
| string.split turns a string into a list of segments of the string according to the string argument. Sort of like cutting DNA along known restriction sequences. What is returned here is a list within a list, the enclosed list being the result of the splice. That way we can recall parts of the string separated by " " (a space) independently. |
| [] is an empty list. list=[] means that the variable "list" has a value of an empty (nothing in it) list. Mark Guzdial |
i don't understand why you are doing the length stuff...why wouldn't you just do what we did in class on Monday? it's so much easier to understand
| Always more than one way to skin a cat! Learn the way that makes the most sense to you and ignore the rest, if you'd like. Or learn all the different ways and figure out how they all get around to doing the same thing. Mark Guzdial |
Link to this Page