trouble understanding None
Peter Otten
__peter__ at web.de
Wed Nov 12 05:48:02 EST 2003
Jakle wrote:
> I'm trying to write a program (with my very limited knowledge of python)
> that will convert text I type into those letters drawn with ascii symbols.
> I did 2 letters then went to test it. Here's the code I have so far:
>
[...]
> WOW, that came out weird, but if you copy/paste it into idle it looks
> fine. That an "S" and a "T". Anyways, The idea is to have a function for
> each letter, then use a for loop and a ton of if statements to traverse
> and print the letters/functions. I understand that I might be doing too
> much work to do this, but I'm trying to practice what I am learning. OK,
> the test prints the letters, but also prints "None" at the end of each
> function. I don't understand it. I'm reading "How To Think Like A Computer
> Scientist: Learning With Python", and it only has one little paragraph
> about the "None" return value, and that's only regarding conditional
> statements. If someone could throw some wisdom my way I'm be very
> greatful. Thanks ahead of time.
Never put print statements into functions that shall themselves produce a
printable result
Be aware that in order to get a single printable backslash, you have to put
it twice into a string constant:
>>> print "\\"
\
For a learning experience it would probably be best to stick with your
approach and just write
S(); T(); S()
instead of
print S(), T(), S()
The next step would then be to put the functions into a dictionary and look
them up:
d = {"S": S, "T": T}
for c in "some string":
d[c]()
However, you did appeal to the "child in the man", so I put together some
code that does what you want but didn't dare ask :-)
I won't go into the details, but the concept is to store an entire line of
text and translate it into the twelve partial lines of your ascii art
charset. With
print >> obj, "some string"
you can redirect the output to any obj that provides a write(s) method.
Peter
import sys
charset = { "S":
[
" ________ ",
" /--------\ ",
"// \\\\",
"|| ^^",
"|| ",
r"\\________ ",
r" \--------\ ",
" \\\\",
" ||",
"_ ||",
r"\\________//",
r" \--------/ ",
],
"T":
[
"______________",
"------ ------",
" || ",
" || ",
" || ",
" || ",
" || ",
" || ",
" || ",
" || ",
" || ",
" || ",
],
" ":
[" "] * 12
}
class Big:
def __init__(self, charset=charset, height=None, write=None,
defaultchar=" "):
self.charset = charset
if height is None:
height = len(charset.itervalues().next())
self.height = height
self.cache = []
self.defaultchar = charset[defaultchar]
if write is None:
write = sys.stdout.write
self.rawWrite = write
def _writeLine(self):
line = [self.charset.get(c, self.defaultchar) for c in
"".join(self.cache)]
self.cache = []
for row in range(self.height):
self.rawWrite("".join([c[row] for c in line]))
self.rawWrite("\n")
def write(self, s):
while True:
pos = s.find("\n")
if pos == -1:
break
self.cache.append(s[:pos])
self._writeLine()
s = s[pos+1:]
self.cache.append(s)
def close(self):
self.write("\n")
big = Big()
print >> big, "ST", "TS"
print >> big, "STS"
print >> big, "S\nT\nS"
print >> big, "TS",
big.close()
More information about the Python-list
mailing list