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