Routine collects variables, creates file, but doesn't write one into other

One of the thing I got some exposure to at sxsw, was programming in Python. I’ve just tried to pick up where I left off over there, but the book we were given clearly pre-dates version 3.4.3 I have installed. With that, I am finding myself debugging changes in Python syntax as I go - perhaps not the most efficient way to learn, even if it does give me some interesting lessons in reading error messages.

The latest exercise is the first on writing content into a simple text file: collect data into a series of variables, open up a new file & name it, write the variables into a string, insert string into file, and close it. I’ve fixed a number of error messages already, and it runs without error, doing everything but insert the string of variables into the file (I know the string is being created, because that’s a part of the output).

Can anyone point me as to what I’m missing, as my reading in the Python manual, isn’t giving me too many pointers?

The code:

name = input("What is your name? ")
email = input("What is your email address? ")
favouriteBand = input("What is your favourite band? ")
outputString = name + (" | ") + email + (" | ") + favouriteBand
fileName = name + (".txt")

#open the file
file = open(fileName, "w")
file.write(outputString)
print (outputString, "is written to file", fileName)
file.close

The output:

What is your name? Chris
What is your email address? chris@example.com
What is your favourite band? Rush
Chris | chris@example.com | Rush is written to file Chris.txt

Is there a Chris.txt file but with nothing written to it, or no Chris.txt file?

The Chris.txt file gets created, but with nothing written to it.

If you try changing to
outputString = name
does it at least write only that?

And should it be close() instead of close?

1 Like

Apparently it should - thanks.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.