Change Contents of the Bubble
Welcome to CS1315. Click on the python to add comments.


This page removed for FERPA compliance
View this PageEdit this Page (locked)Uploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Spring 2004 Homework 6 Questions

Questions? Comments? Issues?




Are we required to use MSNBC? Can we use another site if we want to?

No, sorry – you must use MSNBC. Doing a different website involves finding different cues for where the headlines are. Each term, we do the homework for you to figure out the right cues. You don't want to use a different site and have to figure out the cues yourself. (Unless, of course, you just wanted to use a previous term's HW6, in which case you're especially required to use MSNBC :-) Mark Guzdial


so we go to the main website, find the "more top stories" headline, view the source code for this page, and then copy the code for the headlines of the three top stories so that we can use those 3 codes in our function?
or
are we having like the entire webpage scrolling across our screen??? mmm...need a little help here. :) Thanks
Student930

You ONLY need the text from the top three headlines after "More top stories" scroll by. At this moment, at http://www.msnbc.msn.com, the top 3 MORE TOP STORIES headlines are "19 hostages released in Iraq; still more held", "Ephedra ban goes into effect nationwide", and "Earning optimism propels Wall Street". Those three are to be made into a tickertape movie. We WILL be discussing HW6 in Breakouts this week. Mark Guzdial


Oh, to be clear: You don't go there with your browser. You write a program which, using urllib.urlopen, opens and reads the page. This is a movie you're making with LIVE data. Mark Guzdial

How do we check to see if the program worked? I'm a little confused.

It's just like hw 5. Use movie maker. That's what I did. I'm fnished!

Or just look at your frames. Mark Guzdial


Example from breakout on reading headlines


import urllib

def headlines2():
  con = urllib.urlopen("http://www.msnbc.msn.com")
  page = con.read()
  con.close()
  headlines = page.find("MORE TOP STORIES")
  if headlines == -1:
    print "Can't find it!"
    return
  #If we're still here, we found the MORE TOP STORIES
  startanchor = page.find("<a",headlines)
  startheadline = page.find(">",startanchor)
  endheadline = page.find("</a>",startheadline)
  print "FIRST HEADLINE:",page[startheadline+1:endheadline]
  startanchor = page.find("<a",endheadline)
  startheadline = page.find(">",startanchor)
  endheadline = page.find("</a>",startheadline)
  print "SECOND HEADLINE:",page[startheadline+1:endheadline]



have fun with this one boys and girls, i am done!!!

How are we supposed to find the anchor tag for the top three headlines when the source is so unorganized? Is there some type of efficient search tool we could use to find it?

Please read the code above and try running it. Mark Guzdial


Do we need to include 'a href=' in our program? I don't see it in the example above, but it seems that it would be needed to open the page.
You are NOT generating any HTML in this homework, so why would you need a href=? Mark Guzdial


Could someone please explain tickertapes? I can't get mine to work. What should we be using as the input?

tickertapes are just like animations, but with text. the difference is that to use the code in the book, you have to include a string as an argument as well as the directory. Then the string is what gets written. for hw6 you will need at least 3 strings, one for each headline. Student960


when i load my code it says there is an error with "import urllib". Any thoughts?


Perhaps you might mention what the error message is? "import urllib" should be flush with the left margin, no starting spaces. Mark Guzdial


Hi Mark. The directions say to input a directory as a string, but in the above function, headlines2(), nothing is inputed. What do we input, a 'string', or a 'directory'.
His function was not a complete example for how to do hw6, it was an example for how to get two headlines off the website. There are a couple of changes you will have to make to his code in order for it to do everything hw6 needs to. Student117

My partner and I are not agreeing, so we need some advice. We used
the following inside our tickertape function:
addText(canvas,600-(frame10),100,headlines[0])
addText(canvas,600-(frame10),200,headlines[1])
addText(canvas,600-(frame10),300,headlines[2])
Are the 0, 1, and 2 next to headlines supposed to be 1, 2, and 3?

Should it matter as long as they're all arguments in the function?

I don't know; that's why I'm asking you.

The question is what is the list headlines. If it's a list of the three headline strings, then the right subscripts (to get the first three items) are 0, 1, and 2. Mark Guzdial


Okay, I understand now. Thank you so much.

from the question posted above, about headlines coming from a list...how did you compile your headlines into a list so that you could just call them into your function?


import urllib

def hw6(directory):
  con = urllib.urlopen("http://www.msnbc.msn.com")
  page = con.read()
  con.close()
  headlines = page.find("MORE TOP STORIES")
  startanchor = page.find("",startanchor)
  endheadline = page.find("",startheadline)
  string1="FIRST HEADLINE:",page[startheadline+1:endheadline]
  print string1
  startanchor = page.find("",startanchor)
  endheadline = page.find("",startheadline)
  string2="SECOND HEADLINE:",page[startheadline+1:endheadline]
  print string2
  startanchor = page.find("",startanchor)
  endheadline = page.find("",startheadline)
  string3="THIRD HEADLINE:",page[startheadline+1:endheadline]
  print string3
  tickertape(directory,string1,string2,string3)

def tickertape(directory,string1,string2,string3):
  for frame in range(0,101):
    canvas = makePicture(getMediaPath("640x480.jpg"))
    addText(canvas,600-(frame*5),100,string1)
    addText(canvas,600-(frame*5),200,string2)
    addText(canvas,600-(frame*5),300,string3)
    framenum=str(frame)
    if frame < 10:
      writePictureTo(canvas,directory+"//frame00"+framenum+".jpg")
    if frame >= 10 and frame < 100:
      writePictureTo(canvas,directory+"//frame0"+framenum+".jpg")
    if frame >= 100:
      writePictureTo(canvas,directory+"//frame"+framenum+".jpg")

This is what I have so far. I am getting an error in the first add text line that says, "An attempt was made to call a function with a parameter of an invalid type. This means that you did something such as trying to pass a string to a method that is expecting an integer." Can anybody see what I'm doing wrong?
Look at what you have defined as string1. Is that a string? Try taking of the "FIRSTHEADLINE", part and see what happens... Student117

What does that mean???

Yeah, I am having a similar problem


I need some help. I have the two separate functions; (1) to get the headlines (2) to create the movie. But, for some reason, I am stuck when it comes to putting the two together into one function. I've been trying all kinds of stuff, and I think that I am just 'missing' something. It's probably really simple, but MY brain has run out of ideas! Could someone please give me a suggestion? ttttttthannnnnk you!
Check out how someone has done the code above, you're going to want to call one of your functions in the other. Student117

right, it isn't a string...it is when you print it out. BUT how do you get that string to be called as an input into the next function???
Pass it as input. Mark Guzdial


import urllib

def headlines2(directory):
  con = urllib.urlopen("http://www.msnbc.msn.com")
  page = con.read()
  con.close()
  headlines = page.find("MORE TOP STORIES")
  if headlines == -1:
    print "Can't find it!"
    return
  startanchor = page.find("<a",headlines)
  startheadline = page.find(">",startanchor)
  endheadline = page.find("</a>",startheadline)
  string1="FIRST HEADLINE:",page[startheadline+1:endheadline]
  print string1
  startanchor = page.find("<a",endheadline)
  startheadline = page.find(">",startanchor)
  endheadline = page.find("</a>",startheadline)
  string2="SECOND HEADLINE:",page[startheadline+1:endheadline]
  print string2
  startanchor = page.find("<a",endheadline)
  startheadline = page.find(">",startanchor)
  endheadline = page.find("</a>",startheadline)
  string3="SECOND HEADLINE:",page[startheadline+1:endheadline]
  print string3
  tickertape(directory, string1, string2, string3)


def tickertape(directory,string1,string2,string3):
  for frame in range(0,101):
    canvas = makePicture(getMediaPath("640x480.jpg"))
    addText(canvas,600-(frame*5), 100, string1)
    addText(canvas,600-(frame*5), 200, string2)
    addText(canvas,600-(frame*5), 300, string3)
    framenum=str(frame)
    if frame < 10:
      writePictureTo(canvas,directory+"//frame00"+framenum+".jpg")
    if frame >= 10 and frame < 100:
      writePictureTo(canvas,directory+"//frame0"+framenum+".jpg")
    if frame >= 100:
      writePictureTo(canvas,directory+"//frame"+framenum+".jpg")

I get the error listed below. Does anyone know why? Thanks. Student1192
('FIRST HEADLINE:', 'U.S. pulls diplomats from Saudi Arabia')
('SECOND HEADLINE:', "Italians united in outrage over hostage's killing")
('SECOND HEADLINE:', 'Europeans cool to Bush support of Sharon plan')
An attempt was made to call a function with a parameter of an invalid type. This means that you did something such as trying to pass a string to a method that is expecting an integer.
 in file C:\JES\hw6.py, on line 26, in function headlines2
 in file C:\JES\hw6.py, on line 32, in function tickertape
 in file C:\JES\Sources\media.py, on line 791, in function addText
 in file C:\JES\Sources\media.py, on line 529, in function addText
TypeError: drawString(): 1st arg can't be coerced to String or java.text.AttributedCharacterIterator
Please check line 32 of C:\JES\hw6.py


I don't know what the error means, but the problem with your code is that what you're defining as string1 etc aren't strings, if that helps.

right, it isn't a string...it is when you print it out. BUT how do you get that string to be called as an input into the next function???
Look at your assignments, Karin. string2="SECOND HEADLINE:",page[startheadline+1:endheadline] What's that comma in there for? Commas aren't operators that work on strings. Maybe you want a "+" in there to add two strings together? Or just hack out the "n HEADLINE:" part? Mark Guzdial

What exactly are the inputs that we should put in the tickertape function? I've seen people put a string, or like above, they put a directory, string1, string2, and string3.
hw6 should ONLY take a directory. It's okay to then call a subfunction, maybe named 'tickertape', with additional inputs. Mark Guzdial

I think page 236 will help significantly.

On the turn-in instructions it says to store the frames to files in the input directory. Does this mean we are supposed to turn-in the movie file we made or just the code like we did for hw5?
Just the code. Mark Guzdial

REMOVED 236 doesn't have a tickertape example on it.
REMOVED 267. Mark Guzdial


How to put strings into lists...and get them back out again!

>>> headline1="George Bush likes Broccoli!"
>>> headline2="Cheney goes jogging, and doesn't croak!"
>>> headline3="Clough named Supreme Court Justice"
>>> headlinelist=[headline1,headline2,headline3]
>>> print headlinelist
['George Bush likes Broccoli!', "Cheney goes jogging, and doesn't croak!", 'Clough named Supreme Court Justice']
>>> print headlinelist[0]
George Bush likes Broccoli!



So far I have this as a program:
import urllib
def head2(directory):
  con = urllib.urlopen("http://www.msnbc.msn.com")
  page = con.read()
  con.close()
  headlines = page.find("MORE TOP STORIES")
  if headlines == -1:
    print "can't find it"
    return
  startanchor = page.find("",startanchor)
  endheadline=page.find("",startheadline)
  headline1= "FIRST HEADLINE:",page[startheadline+1:endheadline]
  print headline1
  startanchor = page.find("",startanchor)
  endheadline=page.find("",startheadline)
  headline2="SECOND HEADLINE:",page[startheadline+1:endheadline]
  print headline2
  startanchor = page.find("",startanchor)
  endheadline=page.find("",startheadline)
  headline3= "THIRD HEADLINE:",page[startheadline+1:endheadline]
  print headline3
  headlinelist=[headline1,headline2,headline3]
  tickertape(directory,headlinelist)


def tickertape(directory,headlinelist):
  for frame in range(0,101):
    canvas = makePicture(getMediaPath("640x480.jpg"))
    addText(canvas,600-(frame*5),100,headlinelist[0])
    addText(canvas,600-(frame*5),200,headlinelist[1])
    addText(canvas,600-(frame*5),300,headlinelist[2])
    framenum=str(frame)
    if frame < 10:
      writePictureTo(canvas,directory+"//frame00"+framenum+".jpg")
    if frame >= 10 and frame < 100:
      writePictureTo(canvas,directory+"//frame0"+framenum+".jpg")
    if frame >= 100:
      writePictureTo(canvas,directory+"//frame"+framenum+".jpg")


and I recieved this error:
head2(r"C:\Documents and Settings\NICOLE\Desktop\HW5")
('FIRST HEADLINE:', 'U.S. may reduce forces in Afghanistan')
('SECOND HEADLINE:', 'Former Qwest executives cleared of fraud')
('THIRD HEADLINE:', 'Ernst & REMOVED barred from new business')
('FIRST HEADLINE:', 'U.S. may reduce forces in Afghanistan')
An attempt was made to call a function with a parameter of an invalid type. This means that you did something such as trying to pass a string to a method that is expecting an integer.
Please check line 35 of C:\Documents and Settings\NICOLE\Desktop\HW5\hw6.py
PLEASE HELP!!!!
Paula Verden

try frame*5 instead of frame5 Doug Gladden

since we're making 100 frames, shouldn't the range be from (1,101)?

Paula, you're doing the same thing as Karin. You can't use commas to combine strings. Because you're using a comma, you're creating what's called a tuple/i. The result is that you can only get your headline by saying ttheadlinelist[0][1]/tt. The error is coming up because addText is expecting a string, but headlinelist[0] is a tuple.Mark Guzdial


In the function, do we need to have the strings print like in the above function?Jennifer Garrett_
Also, should they all go across the screen at the same time like that? Or do you want them staggered?Jennifer Garrett_
No, the strings don't have to print. They should all go across at once, at different vertical positions.Mark Guzdial

everything seems to work in mine except the last writePicture To... heres the code
def tickertape(directory,headlinelist):
  for frame in range(1,101):
    canvas = makePicture(getMediaPath("640x480.jpg"))
    addText(canvas,600-(frame*5), 100, headlinelist[0])
    addText(canvas,600-(frame*5), 200, headlinelist[1])
    addText(canvas,600-(frame*5), 300, headlinelist[2])
  writeFrame(frame,directory,canvas)

def writeFrame(num,directory,framepict):
    framenum=str(num)
    if num < 10:
      writePictureTo(framepict,directory+"//frame00"+framenum+".jpg")
    if num >= 10 and framenum < 100:
      writePictureTo(framepict,directory+"//frame0"+framenum+".jpg")
    if num >= 100:
      writePictureTo(framepict,directory+"//frame"+framenum+".jpg")


this is the error from my code above.

writePictureTo(pict,filename): Path is not valid
An error occurred attempting to pass an argument to a function.
in file C:\Documents and Settings\JessLordoftherings\My Documents\testhw6, on line 27, in function headlines2
in file C:\Documents and Settings\JessLordoftherings\My Documents\testhw6, on line 37, in function tickertape
in file C:\Documents and Settings\JessLordoftherings\My Documents\testhw6, on line 46, in function writeFrame
in file C:\Program Files\JES\Sources\media.py, on line 893, in function writePictureTo
ValueError:
Please check line 46 of C:\Documents and Settings\JessLordoftherings\My Documents\testhw6
Your writeFrame call should be INSIDE the loop. Otherwise, you won't be incrementing the frame number. Mark Guzdial

Two questions...why don't the headlines start at the right y-values? Should the frames start at 001 or 000, or does it matter? Thanks. Student1192
Doesn't matter if the frames start at 000 or 001. I don't know why the headlines don't start at the right y values. Mark Guzdial

haha nevermind they are at the right y-values...I was thinking about x-values ;) Student1192

Okay, I'm done but just wanna make sure I have everything. Does it matter what we name our function? Usually you give us a name to give it like "hw5" or "linksREMOVED" or something. Secondly, when we run our movie we should run it at 5 fps? (so it takes 20 sec to go across the screen?) Finally, it shouldn't make it across the screen correct?


When the headlines go across our frames...does it matter if they say FIRST HEADLINE:bla bla bla or SECOND HEADLINE: bla bla bla...?
Lauren Patton




shouldn't we be moving the headlines from right to left? everyone's code dose it from left to right.

Do we need to turn in our empty folder with the blank canvas? Or will the TA's already have one set up? thanks! Student1022

Okay, below is my code. I ran the program and it seemed fine. This is what it printed in my command area:
FIRST HEADLINE:Hamas mourns slain leader, vows revenge
SECOND HEADLINE:Rice: U.S. bracing for attacks before election
SECOND HEADLINE:Fed charges possible in N.D. student’s murder

Is this right? Also, when I went to look at my frames they were completely black. Is this because my font is black and my blank picture is black? Or did I completely do it wrong? PLEASE HELP :(
Belo is my code:


import urllib

def headlines2(directory):
  con = urllib.urlopen("http://www.msnbc.msn.com")
  page = con.read()
  con.close()
  headlines = page.find("MORE TOP STORIES")
  if headlines == -1:
    print "Can't find it!"
    return
  startanchor = page.find("",startanchor)
  endheadline = page.find("",startheadline)
  string1="FIRST HEADLINE:"+page[startheadline+1:endheadline]
  print string1
  startanchor = page.find("",startanchor)
  endheadline = page.find("",startheadline)
  string2="SECOND HEADLINE:"+page[startheadline+1:endheadline]
  print string2
  startanchor = page.find("",startanchor)
  endheadline = page.find("",startheadline)
  string3="THIRD HEADLINE:"+page[startheadline+1:endheadline]
  print string3
  tickertape(directory, string1, string2, string3)


def tickertape(directory,string1,string2,string3):
  for frame in range(0,101):
    canvas = makePicture(getMediaPath("640x480.jpg"))
    addText(canvas,600-(frame*5), 100, string1)
    addText(canvas,600-(frame*5), 200, string2)
    addText(canvas,600-(frame*5), 300, string3)
    framenum=str(frame)
    if frame < 10:
      writePictureTo(canvas,directory+"//frame00"+framenum+".jpg")
    if frame >= 10 and frame < 100:
      writePictureTo(canvas,directory+"//frame0"+framenum+".jpg")
    if frame >= 100:
      writePictureTo(canvas,directory+"//frame"+framenum+".jpg")

Student1113

Headlines going from RIGHT TO LEFT, or LEFT TO RIGHT?

A couple of questions: First, we should have the hw6 function that takes a directory as input and nothing else and that runs the headlines function inside right? Secondly, I have about the same code as everyone else but when it gets to the "headline[0]" part, it says a local or global name could not be found after the function already ran the headlines() function. I do not understand!!

Headlines from right to left; hw6 takes just a directory. What do you mean that you have about the same code? It's hard to help if we can't see it. Stephanie Weitzel


I am having a hard time submitting my code, there is always an unexpected error... what is happening ? and can i just submit my code by email, or something? Melissa Boswell


import urllib

def headlines():
con = urllib.urlopen("http://www.msnbc.msn.com")
page = con.read()
con.close()
headlines = page.find("MORE TOP STORIES")
if headlines == -1:
print "Can't find it!"
return
startanchor = page.find("a",headlines)
startheadline = page.find(">",startanchor)
endheadline = page.find("",startheadline)
firstHeadline = ",page[startheadline+1:endheadline],"
startanchor = page.find("a",endheadline)
startheadline = page.find(">",startanchor)
endheadline = page.find("",startheadline)
secondHeadline = ",page[startheadline+1:endheadline],"
startanchor = page.find("a",endheadline)
startheadline = page.find(">",startanchor)
endheadline = page.find("",startheadline)
thirdHeadline = ",page[startheadline+1:endheadline],"
headLineList = [firstHeadline, secondHeadline, thirdHeadline]

def hw6(directory):
headlines()
for frame in range(1, 101):
canvas = makePicture(getMediaPath("640X480.jpg"))
addText(canvas, 600-(frame5), 100, headLineList[0])
addText(canvas, 600-(frame5), 200, headLineList[1])
addText(canvas, 600-(frame5), 300, headLineList[2])
framenum = str(frame)
if frame 10:
writePictureTo(canvas, directory+"//frame0"+framenum+".jpg")
if frame > 10:
writePictureTo(canvas, directory+"///frame"+framenum+".jpg")


Here is my program. When it gets to the line with "headline[0]", it says no local or global name could be found. I don't know why. Please help.


When you say "it gets to the line with "headline[0]"", I assume you mean "headLineList[0]"? If so, you are defining headLineList in your "headlines()" function, but you are not returning it to your hw6 function. Remember, any variables defined in a function are only visible (and only exist) in that function, so if you want "headLineList" to be used by your hw6() function, you need to return in. Blair MacIntyre

MULTIPLY your frames (frame*5)


I did that... it just didn't show up.

How long is this assignment taking most people? Due to a family emergency, I am just now starting (10:30 pm).


Hi, Jes is giving me this error-
>>> hw6(path)
load image failed.
Was unable to load the image in W:\My Desktop\JES\hw6testfolder\640x480.jpg
Make sure it's a valid image file.
A local or global name could not be found. You need to define the function or variable before you try to use it in any way.
Please check line 27 of \\COA\Users\gtg274f\My Desktop\JES\hw6.py
>>> 
Here's my code: 
import urllib
def hw6(directory):
  #import content from website:
  page = urllib.urlopen("http://www.msnbc.msn.com")
  contents=page.read()
  page.close()
  #finding headlines in the contents of the page:
  headlines=contents.find("MORE TOP STORIES")
  startanchor = contents.find("<a",headlines)
  startheadline=contents.find(">",startanchor)
  endheadline=contents.find("</a>",startheadline)
  H1=contents[startheadline+1:endheadline]
  startanchor=contents.find("<a",endheadline)
  startheadline=contents.find(">",startanchor)
  endheadline=contents.find("</a>",startheadline)
  H2=contents[startheadline+1:endheadline]
  startanchor=contents.find("<a",endheadline)
  startheadline=contents.find(">",startanchor)
  endheadline=contents.find("</a>",startheadline)
  H3=contents[startheadline+1:endheadline]
  Hlist=[H1,H2,H3]
  tickertape(directory,Hlist)

def tickertape(directory,Hlist):
  for frame in range(1,101):
    canvas=makePicture(getMediaPath("640x480.jpg"))
    addText(canvas,600-(frame*5),100,H[0])
    addText(canvas,600-(frame*5),200,H[1])
    addText(canvas,600-(frame*5),300,H[2])
    framenum=str(frame)
    if frame <10:
      writePictureTo(canvas,directory+"//frame0"+framenum+".jpg")
    if frame >=10:
      writePictureTo(canvas,directory+"//frame"+framenum+".jpg")



Did you setMediaPath to the mediasources directory? Is 640x480.jpg in the directory W:\My Desktop\JES\hw6testfolder? Blair MacIntyre

Yes the blank picture is in the W:\My Desktop\JES\hw6testfolder

import urllib

def hw6(directory):
  con = urllib.urlopen("http://www.msnbc.msn.com")
  page = con.read()
  con.close()
  headlines = page.find("MORE TOP STORIES")
  if headlines == -1:
    print "Can't find it!"
    return
  #If we're still here, we found the MORE TOP STORIES
  startanchor = page.find("<a",headlines)
  startheadline = page.find(">",startanchor)
  endheadline = page.find("</a>",startheadline)
  headline1= "+page[startheadline+1:endheadline]"
  startanchor = page.find("<a",endheadline)
  startheadline = page.find(">",startanchor)
  endheadline = page.find("</a>",startheadline)
  headline2= "+page[startheadline+1:endheadline]"
  startanchor = page.find("<a",endheadline)
  startheadline = page.find(">",startanchor)
  endheadline = page.find("</a>",startheadline)
  headline3="+page[startheadline+1:endheadline]"
  headlinelist = [headline1, headline2, headline3]
  for frame in range(0,101):
    canvas = makePicture(getMediaPath("640x480.jpg"))
    addText(canvas,600-(frame*5),100,headLineList[0])
    addText(canvas,600-(frame*5),200,headLineList[1])
    addText(canvas,600-(frame*5),300,headLineList[2])
    writeFrame(frame, directory, canvas)

def writeFrame(num,directory,framepict):
    if frame < 10:
      writePictureTo(canvas,directory+"//frame00"+framenum+".jpg")
    if frame >= 10 and frame < 100:
      writePictureTo(canvas,directory+"//frame0"+framenum+".jpg")
    if frame >= 100:
      writePictureTo(canvas,directory+"//frame"+framenum+".jpg")


Ok, I keep getting an error when it gets to the line with headlinelist[0] in it. It says a local or global name cannot be found. What is the problem? And are the + signs in the right places on my headline1, headline2, and headline3 lines?
Student966

How do we put the headline strings into a list? The example given earlier in the page had them in brackets, separated by commas. But then Prof. Guzdial said that you can't separate them by commas without creating a tuple. So how do we put them in a list, and how do we reference each headline from the the list once we do?
Ex.
headlineList= [headline1, headline2, headline3]


import urllib

def hw6(directory):
  con = urllib.urlopen("http://www.msnbc.msn.com")
  page = con.read()
  con.close()
  headlines = page.find("MORE TOP STORIES")
  if headlines == -1:
    print "Can't find it!"
    return
  #If we're still here, we found the MORE TOP STORIES
  startanchor = page.find("<a",headlines)
  startheadline = page.find(">",startanchor)
  endheadline = page.find("</a>",startheadline)
  headline1= "FIRST HEADLINE:"+page[startheadline+1:endheadline]
  startanchor = page.find("<a",endheadline)
  startheadline = page.find(">",startanchor)
  endheadline = page.find("</a>",startheadline)
  headline2= "SECOND HEADLINE:"+page[startheadline+1:endheadline]
  startanchor = page.find("<a",endheadline)
  startheadline = page.find(">",startanchor)
  endheadline = page.find("</a>",startheadline)
  headline3="THIRD HEADLINE:"+page[startheadline+1:endheadline]
  headlinelist = [headline1, headline2, headline3]
  return headlinelist
  tickertape (directory, headlinelist)

def tickertape(directory, headlinelist):
  for frame in range(0,101):
    canvas = makePicture(getMediaPath("640x480.jpg"))
    addText(canvas,600-(frame*5),100,headLineList[0])
    addText(canvas,600-(frame*5),200,headLineList[1])
    addText(canvas,600-(frame*5),300,headLineList[2])
    writeFrame(frame, directory, canvas)
    if frame < 10:
      writePictureTo(canvas,directory+"//frame00"+framenum+".jpg")
    if frame >= 10 and frame < 100:
      writePictureTo(canvas,directory+"//frame0"+framenum+".jpg")
    if frame >= 100:
      writePictureTo(canvas,directory+"//frame"+framenum+".jpg")


I changed stuff around a little, but now I cannot get my frames to write to the directory. What am I doing wrong?

Student966

Anu, one problem that I have had in the past, and I don't know if this is relevant or not, but having spaces in the directory name has had adverse effects on my programs... like not running them correctly. Maybe you could set up a different test directory on your actual c drive.

I am using a computer in the college of architecture and it doesn't allow me to access the C drive. So I guess I'll try it from home. Thank you.

Heather, you have "writeFrame" being called, before any of your writePictureTo commands, but writeFrame is not defined. Blair MacIntyre

import urllib

def hw6(directory):
  con = urllib.urlopen("http://www.msnbc.msn.com")
  page = con.read()
  con.close()
  headlines = page.find("MORE TOP STORIES")
  if headlines == -1:
    print "Can't find it!"
    return
  #If we're still here, we found the MORE TOP STORIES
  startanchor = page.find("<a",headlines)
  startheadline = page.find(">",startanchor)
  endheadline = page.find("</a>",startheadline)
  headline1= "FIRST HEADLINE:",page[startheadline+1:endheadline]
  startanchor = page.find("<a",endheadline)
  startheadline = page.find(">",startanchor)
  endheadline = page.find("</a>",startheadline)
  headline2= "SECOND HEADLINE:",page[startheadline+1:endheadline]
  startanchor = page.find("<a",endheadline)
  startheadline = page.find(">",startanchor)
  endheadline = page.find("</a>",startheadline)
  headline3="THIRD HEADLINE:",page[startheadline+1:endheadline]
  headlinelist = [headline1, headline2, headline3]
  return headlinelist
  tickertape (directory, headlinelist)

def tickertape(directory, headlinelist):
  for frame in range(0,101):
    canvas = makePicture(getMediaPath("640x480.jpg"))
    addText(canvas,600-(frame*5),100,headLineList[0])
    addText(canvas,600-(frame*5),200,headLineList[1])
    addText(canvas,600-(frame*5),300,headLineList[2])
    writeFrame(frame, directory, canvas)
   

def writeFrame(num,directory,framepict):
    if frame < 10:
      writePictureTo(canvas,directory+"//frame00"+framenum+".jpg")
    if frame >= 10 and frame < 100:
      writePictureTo(canvas,directory+"//frame0"+framenum+".jpg")
    if frame >= 100:
      writePictureTo(canvas,directory+"//frame"+framenum+".jpg")


ok i defined writeFrame, but my program only runs until the return headlinelist command. It doesn't keep going to write out the frames and do the tickertape. Why is that? And I know I am not supposed to use commas on the headline1 =, headline2=, etc lines but I didn't understand the explanation of what to do instead.
Student966

Heather, return always ends the function. Why are you using it there? Your earlier code used "+" in your headline strings – why did you switch back to commas? Mark Guzdial


ok i fixed the commas again. So how do I make headlinelist work in my tickertape function if I don't return it in the hw6 function?
Student966
Why do you need to return it at all? What do you think it's doing. Mark Guzdial



This is my code:

import urllib

def hw6(directory):
con = urllib.urlopen("http://www.msnbc.msn.com")
page = con.read()
con.close()
headlines = page.find("MORE TOP STORIES")
if headlines == -1:
print "Can't find it!"
return
startanchor = page.find("",startanchor)
endheadline = page.find("",startheadline)
string1="FIRST HEADLINE:"+page[startheadline+1:endheadline]
print string1
startanchor = page.find("",startanchor)
endheadline = page.find("",startheadline)
string2="SECOND HEADLINE:"+page[startheadline+1:endheadline]
print string2
startanchor = page.find("",startanchor)
endheadline = page.find("",startheadline)
string3="THIRD HEADLINE:"+page[startheadline+1:endheadline]
print string3
tickertape(directory, string1, string2, string3)


def tickertape(directory,string1,string2,string3):
for frame in range(0,101):
canvas = makePicture(getMediaPath("640x480.jpg"))
addText(canvas,600-(frame5), 100, string1)
addText(canvas,600-(frame5), 200, string2)
addText(canvas,600-(frame5), 300, string3)
framenum=str(frame)
if frame 10:
writePictureTo(canvas,directory+"//frame00"+framenum+".jpg")
if frame >= 10 and frame 100:
writePictureTo(canvas,directory+"//frame0"+framenum+".jpg")
if frame >= 100:
writePictureTo(canvas,directory+"//frame"+framenum+".jpg")

This is the error I get:
A local name was used before it was created. You need to define the method or variable before you try to use it.
Please check line 11 of \\COA\Users\gtg632r\My Desktop\firstattempt

Why is that? I've been trying to change things for hours...
Andre Escudero
First thing: Turn on expert mode (Options in the Edit menu) so that you can see what local name it's complaining about. Second: It's going to tell you it's startheadline, which you use but never define. Mark Guzdial


MSNBC JUST CHANGED

If you visit http://www.msnbc.msn.com right now (10:57 am on April 19), you'll find that there are TWO "MORE TOP STORIES" sections, and the first one only has TWO headlines. If you run your code that worked fine on Friday today, it won't work.

Don't panic! Things like this happen. REMOVED designers won't guarantee that they don't change their sites that will screw up your programs.

First, if you already turned in your code, you're fine. My guess is that that first "MORE TOP STORIES" will go away by the time that the TA's test it, and even if it doesn't, you won't be penalized for designing for the OLD version of the page.

Second, for those of you who are trying to get your HW6 working today, you have to do something slightly different to make everything work with the new MSNBC format. Here are two options:

OPTION #1: DO AN rfind

This version of my example will work with both the new and old formats of MSNBC. (In other words, it'll still work when they fix things.) I'm simply doing an "rfind" instead of a "find". That does a "reverse find" (from the end of the page), and thus finds the second "MORE TOP STORIES" rather than the first.

import urllib

def headlines2():
  con = urllib.urlopen("http://www.msnbc.msn.com")
  page = con.read()
  con.close()
  headlines = page.rfind("MORE TOP STORIES")
  if headlines == -1:
   print "We are hosed."
   return
  # If we get here, we found MORE TOP STORIES
  startanchor = page.find("<a",headlines)
  startheadline = page.find(">",startanchor)
  endheadline = page.find("</a>",startheadline)
  print "Headline #1:",page[startheadline+1:endheadline]
  startanchor = page.find("<a",endheadline)
  startheadline = page.find(">",startanchor)
  endheadline = page.find("</a>",startheadline)
  print "Headline #2:",page[startheadline+1:endheadline]


OPTION #2: Look for the SECOND "MORE TOP STORIES"

Do this by doing TWO searches, starting the second one from where the first one ends. This will work right now, but will break as soon as MSNBC gets rid of the earlier "MORE TOP STORIES."

import urllib

def headlines2():
  con = urllib.urlopen("http://www.msnbc.msn.com")
  page = con.read()
  con.close()
  headlines = page.find("MORE TOP STORIES")
  headlines = page.find("MORE TOP STORIES",headlines)
  if headlines == -1:
   print "We are hosed."
   return
  # If we get here, we found MORE TOP STORIES
  startanchor = page.find("<a",headlines)
  startheadline = page.find(">",startanchor)
  endheadline = page.find("</a>",startheadline)
  print "Headline #1:",page[startheadline+1:endheadline]
  startanchor = page.find("<a",endheadline)
  startheadline = page.find(">",startanchor)
  endheadline = page.find("</a>",startheadline)
  print "Headline #2:",page[startheadline+1:endheadline]


It won't use headline[0] or any of the others in my tickertape function because they are not defined. I am confused. How do I do that?
Student966



OK MAJOR CRISIS!!!!
my laptop with my Jes program and HW6 has been severly damaged. The screen will not show anything and I tried to get it repaired this weekend but the computer store said that they can not get me another screen for 7-10 days and I have to pay for it. I have no way of cheking my program to even see that it works or sending it what do I need to do. Paula Verden

Is is okay if we have first headline:~~~~~~, second headline:~~~~~~~, etc. in the movie? Someone else asked this question earlier but it wasn't answered. Thanks! Student1192

This is wierd... My program gives no errors, and it chugs along like its making the frames. However, I can't find them. I have my media path set to a folder with a copy of the 640x480.jpg in it, and I'm inputting that folder at the directory. I can't seem to find the frames though.
import urllib

def hw6(directory):
  con = urllib.urlopen("http://www.msnbc.msn.com")
  page = con.read()
  con.close()
  headlines = page.rfind("MORE TOP STORIES")
  if headlines == -1:
   print "We are hosed."
   return
  # If we get here, we found MORE TOP STORIES
  startanchor = page.find("<a",headlines)
  startheadline = page.find(">",startanchor)
  endheadline = page.find("</a>",startheadline)
  headline1="Headline #1:"+page[startheadline+1:endheadline]
  startanchor = page.find("<a",endheadline)
  startheadline = page.find(">",startanchor)
  endheadline = page.find("</a>",startheadline)
  headline2="Headline #2:"+page[startheadline+1:endheadline]
  startanchor = page.find("<a",endheadline)
  startheadline = page.find(">",startanchor)
  endheadline = page.find("</a>",startheadline)
  headline3="Headline #3:"+page[startheadline+1:endheadline]
  headlinelist=[headline1,headline2,headline3]
  print headlinelist
  for frame in range(0,101):
    headlinelist = [headline1, headline2, headline3]
    canvas = makePicture(getMediaPath("640x480.jpg"))
    addText(canvas,600-(frame*5),100,headlinelist[0])
    addText(canvas,600-(frame*5),200,headlinelist[1])
    addText(canvas,600-(frame*5),300,headlinelist[2])
  framenum=str(frame)
  if frame < 10:
    writePictureTo(canvas,directory+"//frame00"+framenum+".jpg")
  if frame >= 10 and frame < 100:
    writePictureTo(canvas,directory+"//frame0"+framenum+".jpg")
  if frame >= 100:
    writePictureTo(canvas,directory+"//frame"+framenum+".jpg")

Ok, it's me again. Help please I have to go to work soon and I won't be back before 5! I have tried everything I can think of. This code runs but it will only write out the 100th frame to my directory not all of them. And i have no subfunctions because whenever I try to make a subfunction it tells me that I have not defined something. How do I do that?
Student966
this is the error that my computer is giving me, after replacing the new code(the rfind code) in place of the code I had been working on:

Headline #1: U.S. creating domestic security task force
Headline #2: Bush scolds Spanish PM over Iraq pullout
A local or global name could not be found. You need to define the function or variable before you try to use it in any way.
in file C:\Documents and Settings\Administrator\My Documents\cs\hw6.py, on line 26, in function headlines2
NameError: start1
Please check line 26 of C:\Documents and Settings\Administrator\My Documents\cs\hw6.py

I don't understand what it means, or what the problem is. line 26 states :

addText(canvas,600-(frame10),100,page[start1 + 1:end1])


Heather, I think I see why you only get the 100th frame. If you'll tab each line once starting with the line: framenum=str(frame)it will put the if statements in the for loop. -Stew

Any ideas why mine won't work?
import urllib

def hw6(directory):
  con = urllib.urlopen("http://www.msnbc.msn.com")
  page = con.read()
  con.close()
  headlines = page.rfind("MORE TOP STORIES")
  if headlines == -1:
    print "Can't find it!"
    return
  startanchor = page.find("",startanchor)
  endheadline = page.find("",startheadline)
  string1 = "Headline 1:"+page[startheadline+1:endheadline]
  print string1
  startanchor = page.find("",startanchor)
  endheadline = page.find("",startheadline)
  string2 = "Headline 2:"+page[startheadline+1:endheadline]
  print string2
  startanchor = page.find("",startanchor)
  endheadline = page.find("",startheadline)
  string3 = "Headline 3:"+page[startheadline+1:endheadline]
  print string3
  headlinelist = [string1, string2, string3]
  tickertape(directory, headlinelist)

def tickertape(directory, headlinelist):
  for frame in range(0,101):
    canvas = makePicture(getMediaPath("640x480.jpg"))
    addText(canvas,600-(frame*5), 100, headlinelist[0])
    addText(canvas,600-(frame*5), 200, headlinelist[1])
    addText(canvas,600-(frame5), 300, headlinelist[2])
    framenum=str(frame)
  if frame < 10:
    writePictureTo(canvas,directory+"//frame00"+framenum+".jpg")
  if frame >= 10 and frame < 100:
    writePictureTo(canvas,directory+"//frame0"+framenum+".jpg")
  if frame >= 100:
    writePictureTo(canvas,directory+"//frame"+framenum+".jpg")

This is the error.
Headline 1:U.S. creating domestic security task force
Headline 2:Bush scolds Spanish PM over Iraq pullout
Headline 3:Palestinian rockets strike Gaza settlements
writePictureTo(pict,filename): Path is not valid
An error occurred attempting to pass an argument to a function.
 in file C:\JES\hw6.py, on line 27, in function hw6
 in file C:\JES\hw6.py, on line 41, in function tickertape
 in file C:\JES\JES\Sources\media.py, on line 894, in function writePictureTo
ValueError: 
Please check line 41 of C:\JES\hw6.py 
+Thomas Vedrody+


Thomas, you need to look at what you have set string1, string2, and string3 to...why not try deleting the part in " "s and just having the second part? Ashley Coker P.S. This question was answered above.

Oh my god... I just say here for the past 4 hours with working code, except the folder I was trying to use was mediasources\test. I finally asked my coding nut roomate for help, and he figured out that \t is an escape command(something like that) so it was messing up my path. ARGH... I never would have figured that out.

btw, it was fixed by adding double slashes into my input directory. example: jes\\mediasources\\test



You could have also put r in front of your string, like we suggest at the beginning of the book... :-) Mark Guzdial




Link to this Page