0

I'm working through Learn Python the Hard Way, and trying to understand it rather than just hammer away. I got stuck on Exercise 16, as discussed already on SO here:

Very basic Python question (strings, formats and escapes)

but I'm still trying to figure out why this approach does not work:

from sys import argv

script, filename = argv


print "Attempting to open the file now." 
print open(filename).read()

print "We're going to erase %r." % filename
print "If you don't want that, hit CTRL-C."

print "If you do want that, hit RETURN." 

raw_input("?")

print "Opening the file..."
target = open(filename, 'w')

print "Truncating the file. Goodbye!"
target.truncate()

print "Now I'm going to ask you for three lines." 

line1 = raw_input("line 1: ")
line2 = raw_input("line 2: ")
line3 = raw_input("line 3: ")

print "I'm going to write these to the file." 

linebreak = "\n"
target.write("%s %s %s %s %s %s") % (line1, linebreak, line2, linebreak, line3, linebreak)

target.write("the ending line")

print "And finally, we close it." 
target.close()

I've established a value for linebreak, and am calling the line1, line2 and linebreak values with %s in the target.write command. Should't it parse as "line1 \n line2 \n line3 \n" when it's read?

This is probably the equivalent of being asked by a child what keeps the sky up or something, and I apologize for being kind of thick. Thanks!

Community
  • 1
  • 1
JeanSibelius
  • 1,529
  • 3
  • 14
  • 27
  • 2
    Can you elaborate on "doesn't work"? Do you get an exception? If so, please provide it. If not, please explain what you're expecting and what you're actually receiving. – g.d.d.c Aug 29 '11 at 17:03
  • 1
    And on the off chance I can guess at it - is it possible that when you re-open the file to verify its contents that the editor you're using doesn't recognize `\n` as a newline and that it is in fact looking for `\r\n` (carriage return + line feed)? This is common in Notepad on Windows, for example. – g.d.d.c Aug 29 '11 at 17:08
  • Apologies -- I have been getting "TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple'" when it reaches the target.write portion of the script. – JeanSibelius Aug 29 '11 at 17:11

4 Answers4

9
target.write("%s %s %s %s %s %s") % (line1, linebreak, line2, linebreak, line3, linebreak)

should be

target.write("%s %s %s %s %s %s" % (line1, linebreak, line2, linebreak, line3, linebreak))

but would be better written as:

target.write(' '.join(line1, linebreak, line2, linebreak, line3, linebreak))
Dan D.
  • 73,243
  • 15
  • 104
  • 123
7

Assuming that you're getting

TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple'

What you need is

target.write("%s %s %s %s %s %s" % (line1, linebreak, line2, linebreak, line3, linebreak))

That is, you need to use the % operator on the string, not on the result of target.write(). The error message might make more sense to you if you realise that target.write() returns None, which has type NoneType.

Martin Stone
  • 12,682
  • 2
  • 39
  • 53
0

I did this a couple days ago, in exercise 22 he will ask you to write down everything you have learned so far and memorize it.

Earlier he asked that you comment every line to explain what it does. That is probably a really good habit until you just know what the lines do without having to think about them.

Also be careful about how you state things.

If you want to become a programmer you need to start talking like one and using the vocabulary.

line1, line2, line3, linebreak are called variables. You temporarily give them/assign them values using the = (assignment operator).

Write is a function. Functions in python take arguments in between (). All of the arguments have to fit inside the (). If you wrote it like this for instance I'm sure it wouldn't have tripped you up.

stuffIWantToPrint = line1 + lb + line2 + lb+ line3 + lb #all strings together 
target.write(stuffIWantToPrint) #pass the big string to write
user2415706
  • 932
  • 1
  • 7
  • 19
0

I'm very late but you, also do this: target.write("{line1}\n{line2}\n{line3}\n").format(line1=line1,line2=line2,line3=line3)

Mr.Zeus
  • 424
  • 8
  • 25