Newbie programmer needs a little help

Alex Martelli aleaxit at yahoo.com
Fri Apr 20 13:03:00 EDT 2001


"Lars Urlich" <wildchild07770 at yahoo.com> wrote in message
news:mailman.987719961.17028.python-list at python.org...
    [snip]
> filename = raw_input("Please enter a path/filename: ")
> filename = open

The second one of these lines re-binds variable
'filename' -- it's now bound to function object
also called 'open', and whatever the user did
enter is forgotten.  Remove the second one of
these two statements... it serves no purpose!

> print "Following are several character options"
> playername = raw_input("What is your name? ")
> charname = raw_input("Enter your character name: ")
> nature = raw_input("What is the nature of your
> character? ")
> demeanor = raw_input("What is your demeanor? ")
> info = [playername, charname, nature, demeanor]
> print info

So far so good. but now:


> file = open(filename, list[0:3] "r")

the "list[0:3]" part is incomprehensible to me.
And the "r" is incorrect if you want to WRITE.

file = open(filename, "w")

is probably what you want here.

> filename, "r"

This expression just gets thrown away.  Nothing
has been written.  You need to write the strings
in the list 'info' to the file you've opened!
Assuming you want newline characters after each
one of them, for example:

for line in info:
    file.write(line)
    file.write('\n')

and now that you're done, you'd better also do:

file.close()

(The last isn't strictly necessary, but...).


Alex






More information about the Python-list mailing list