[Tutor] Two questions
Lloyd Kvam
pythontutor@venix.com
Sat, 13 Apr 2002 10:04:25 -0400
You need to create a (possibly empty) list before you can stick
something into the list. This should fix your problem:
i = 0
site_url = [] # create an empty list
site_url[i] = fileobject.readline()
print "site_url = ", site_url[i]
It is usually simpler to not track the positions when putting
things into the list. Something like:
site_url = []
site_url.append( fileobject.readline())
simply adds the url to the end of the list. You can NOT add
things to a tuple. A tuple can NOT be changed. If you want your
URLs to be in a tuple, create the list first. Then use the
builtin tuple function.
tuple_site_url = tuple( site_url) # makes tuple from list
Henry Steigerwaldt wrote:
> To Whom It May Concern:
>
>
>
> The program I am about to describe is opened in Idle, and then run in
>
> Idle. The code pieces below are all located in a function called Main().
>
>
>
> The following piece of code works just fine:
>
>
>
> # read first line in a file to get URL
>
> site_url = fileobject.readline()
>
> print "site_url = ", url
>
>
>
> In the above code, the URL read in first line of a file stores just fine
>
> into variable "site_url." However, when I try to store the URL into a
>
> list or tuple, I get an error.
>
>
>
> Below is the piece of code to store URL into a list:
>
>
>
> i = 0
>
> site_url[i] = fileobject.readline()
> print "site_url = ", site_url[i]
>
> Here is the error that is output in Idle:
>
>
>
> Main()
> File "C:\python_pgms\misc_obs_pgm.py", line 37, in Main
> site_url[i] = fileobject.readline()
> NameError: global name 'site_url' is not defined
>
> _________________________________
>
> Below is the piece of code to store URL into a tuple:
>
>
>
> i = 0
>
> site_url(i) = fileobject.readline()
> print "site_url = ", site_url(i)
>
>
>
> Here is the error that is output in Idle:
>
>
>
> File "C:\python_pgms\misc_obs_pgm.py", line 37
> site_url(i) = fileobject.readline()
> SyntaxError: can't assign to function call
> ______________________________________________
>
>
>
> I do not understand why I am getting these errors.
>
>
>
> Actually what I want to do in this program is read the first
>
> line of a file, which contains a number, say 4. The number tells
>
> the program how many URLs will follow (one per line) in the file.
>
> So after the first line of the file is read, the program then is
>
> supposed to read and then store the URLs in either a list or
>
> a tuple. After that, I want to go to each site (URL) and grab
>
> some information.
>
>
>
> Please be as least cryptic as possible in any response. I have
>
> written some small programs in Tcl/Tk (and a couple of other
>
> languages in the past), but my programming experience is very
>
> limited.
>
>
>
> Thanks for any help provided !
>
>
>
> Henry Steigerwaldt
>
> Hermitage, TN
>
>
>
--
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358
voice:
603-443-6155
fax:
801-459-9582