[Tutor] syntax error that i cant spot!

Noah Hall enalicho at gmail.com
Sat Jan 1 20:52:55 CET 2011


>
> > I am caught off guard but what is the purpose of the plus sign?  I don't
> recall seeing it used like that.
>

The + sign there is used for concating two strings together, for example -

output = 'foo' + 'bar'

Will give the variable output the value of the characters 'foobar'. This
also works with mixed values of variables holding strings and raw strings,
for example -

foo = 'foo'
output = foo + 'bar'

Which gives the same output as my previous example.

To OP:

I would not use the method you have used, but rather use string formatting,
so I would use on line 10 -

print 'Well, %s, I am thinking of a number between 1 & 20.' % (myName)

And on line 30 (which won't need the str(guessesTaken) on line 29) -

print 'Good job, %s! You guessed the number in %d guesses!' % (myName,
guessesTaken)

And then finally on line 34 -

print 'Nope. The number I was thinking of was %d' % (number)


By the way, for readability, tryToavoidCamelCase.
I also recommend you look into functions, classes, and exceptions - this
example is a good one with a lot of room to grow :)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110101/07ca3311/attachment.html>


More information about the Tutor mailing list