[Tutor] What's the invalid syntax? Code supplied

Bob Gailer bgailer at sbcglobal.net
Thu Aug 4 03:48:29 CEST 2005


At 06:21 PM 8/3/2005, Nathan Pinno wrote:
>Hey all,
>
>What wrong with the following syntax?
>
>def save_file(sitelist,filename):
>     out_file = open(filename,"w")
>     for site in sitelist.keys():
>         out_file.write(site+","+sitelist[site][0]+","+sitelist[site][1]"\n")

sitelist[site][1]"\n" is not valid. I think you want sitelist[site][1] + "\n"

>     out_file.close()

You can simplify things by using items():
  for key, value in sitelist.items():
      out_file.write(key +","+value[0] +","+value[1]+"\n")

% formatting can make it even nicer:
      out_file.write("%s,%s,%s\n" % (key, value[0], value[1])

>It highlighted the last " if that will help any.
>
>I can't show you the error due to the fact that the error appeared in a 
>separate box, instead of in the IDLE window.
>
>If you need the rest of the code, just ask. It ran perfect before I added 
>file I/O.
>
>Thanks in advance,
>Nathan Pinno
>Crew, Camrose McDonalds and owner/operator of Woffee

Just out of curiosity, would you explain "Crew, Camrose McDonalds and 
owner/operator of Woffee"

Bob Gailer
phone 510 978 4454  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050803/3862e8ca/attachment.htm


More information about the Tutor mailing list