473,322 Members | 1,241 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

trouble understanding None

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:

*******************************************
def S():
print " ________ "
print " /--------\ "
print "// \\"
print "|| ^^"
print "|| "
print "\\________ "
print " \--------\ "
print " \\"
print " ||"
print "_ ||"
print "\\________//"
print " \--------/ ",

def T():
print "______________"
print "------ ------"
print " || "
print " || "
print " || "
print " || "
print " || "
print " || "
print " || "
print " || "

print S(), T()
*******************************************

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.
Jul 18 '05 #1
9 1591
Edo
You should not print the function result, just invoke them.

Ed.

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:

*******************************************
def S():
print " ________ "
print " /--------\ "
print "// \\"
print "|| ^^"
print "|| "
print "\\________ "
print " \--------\ "
print " \\"
print " ||"
print "_ ||"
print "\\________//"
print " \--------/ ",

def T():
print "______________"
print "------ ------"
print " || "
print " || "
print " || "
print " || "
print " || "
print " || "
print " || "
print " || "

print S(), T()
*******************************************

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.


Jul 18 '05 #2
On Wed, 12 Nov 2003 02:56:43 GMT, Jakle wrote:
WOW, that came out weird, but if you copy/paste it into idle it looks
fine.


It looks fine in any newsreader and editor that uses a fixed-pitch font.
This is highly recommended, since you know that the spacing of the text
you type will be the same for any other fixed-pitch font (as used by
other people). This can't be said of any proportional font.

--
\ "Marriage is a wonderful institution, but who would want to |
`\ live in an institution." -- Henry L. Mencken |
_o__) |
Ben Finney <http://bignose.squidly.org/>
Jul 18 '05 #3
On Wed, 12 Nov 2003 02:56:43 GMT, Jakle wrote:
def S():
# [print a bunch of stuff]

def T():
# [print a bunch of other stuff]

print S(), T()
*******************************************

OK, the test prints the letters, but also prints "None" at the end of
each function.


That's because you've asked for the following sequence of events:

- Invoke S()...
- which prints a bunch of stuff
- then returns None.
- Print the return value of S().
- Invoke T()...
- which prints a bunch of other stuff
- then returns None.
- Print the return value of T().

If you just want the functions invoked (called) instead of getting their
return value and printing it, then do that.

Define the function:
def S(): ... print "Bunch of stuff"
...

Print the return result of S(), which necessitates calling the function
and doing whatever is inside it:
print S() Bunch of stuff
None

Call the function, thus doing whatever is inside it, then throw away the
return value:
S()

Bunch of stuff
Please try to get into the practice of reducing the problem you want to
describe to a minimal working example. This often has the side effect
that you understand the problem better, and don't end up needing to
post it. In the cases where that doesn't happen, at least you've got
something that doesn't have irrelevant extra code in it.

--
\ "If you ever drop your keys into a river of molten lava, let |
`\ 'em go, because, man, they're gone." -- Jack Handey |
_o__) |
Ben Finney <http://bignose.squidly.org/>
Jul 18 '05 #4
Ben Finney wrote:
...
Please try to get into the practice of reducing the problem you want to
describe to a minimal working example. This often has the side effect
that you understand the problem better, and don't end up needing to
post it. In the cases where that doesn't happen, at least you've got
something that doesn't have irrelevant extra code in it.


Excellent advice! For lots more excellent advice on how to best ask
questions on Usenet and technical mailing lists, also see

http://www.catb.org/~esr/faqs/smart-questions.html
Alex

Jul 18 '05 #5
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()

Jul 18 '05 #6
Jakle:
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.


BTW, if you get really into this you might want to take a look
at figlet (http://www.figlet.org/ ) which both does this and provides
fonts for you to do your own ASCII graphics.

Andrew
da***@dalkescientific.com
Jul 18 '05 #7

"Peter Otten" <__*******@web.de> wrote in message
news:bo*************@news.t-online.com...
import sys
charset = { "S":
[
" ________ ",
" /--------\ ",
"// \\\\",
"|| ^^",
"|| ",
r"\\________ ",
r" \--------\ ",
" \\\\",
" ||",
"_ ||",
r"\\________//",
r" \--------/ ",
],


Unless the OP actually needs a list of lines, I think I would make the
value corresponding to each letter one string with embedded newlines:
bigletter = {
'S': r'''
________
/--------\
// \\
|| ^^
||
\\________
\--------\
\\
||
_ ||
\\________//
\--------/'''

# etc
}
print bigletter['S']

This is very easy to edit (with a fixed pitch editor), aud to use.
print bigletter['S']


________
/--------\
// \\
|| ^^
||
\\________
\--------\
\\
||
_ ||
\\________//
\--------/

The only real problem is (maybe) the extra newline at the beginning of
the file, which makes the first line line-up with the rest. The raw
mode input makes it impossible (as far as I know) to escape it (with
the usual '\', which gets printed instead). So one could either
postprocess the dict to slice all values or delete the initial newline
in the source code after getting the letter right.

Terry J. Reedy
Jul 18 '05 #8
Terry Reedy wrote:
Unless the OP actually needs a list of lines, I think I would make the
value corresponding to each letter one string with embedded newlines: [...] This is very easy to edit (with a fixed pitch editor), aud to use.
Yes, this would be a cleaner approach.
The only real problem is (maybe) the extra newline at the beginning of
the file, which makes the first line line-up with the rest. The raw
mode input makes it impossible (as far as I know) to escape it (with
the usual '\', which gets printed instead). So one could either
postprocess the dict to slice all values or delete the initial newline
in the source code after getting the letter right.


I mixed normal and raw strings because I recalled a problem with backslashes
at the end of a raw string. I've since found out that this applies only to
a single last backslash at the end of the string:
r"\\" '\\\\' r"\" File "<stdin>", line 1
r"\"
^
SyntaxError: EOL while scanning single-quoted string


For my Big writer class , that somewhat extends the original task,
postprocessing would be the way to go, because of other problems:

- every line of a character must be of the same length
- every character must have the same height
- if character definitions with different heights are allowed, additional
information about the baseline is required for the program to decide where
to fill in the blank lines
Peter
Jul 18 '05 #9
Peter Otten wrote:
...
I mixed normal and raw strings because I recalled a problem with
backslashes at the end of a raw string. I've since found out that this
applies only to a single last backslash at the end of the string:


Nope, any ODD number of backslashes at the end (1, 3, 5, 7, ...).
Alex

Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Freddie | last post by:
Hi, I've been mangling python-irclib into an asyncore class, so it fits in nicely with the rest of my app. I ran into a problem with asyncore.dispatcher_with_send (Python 2.3.4), though. Not...
2
by: Samuele Giovanni Tonon | last post by:
hi, i'm trying to develop a trivial application which random copy files from a directory to another one. i made it using pygtk for the graphical interface, however i find some problem with...
7
by: CheGueVerra | last post by:
First of all Hello all you css freak. geeks and gurus. I just started using css for some web pages I had to do at work and I'im testing some stuff at home to understand more. Now, I wanted to...
5
by: 2obvious | last post by:
During the window.onload event, I set the .onclick event of an element to turn off the display of the first two elements in a <div> called "content": var objNode =...
6
by: Hypo | last post by:
Im relatilvly new to a web programming in general, and here's the situation i have: I have a default page with dynamic content, and one button with onclick code something like this: { // do...
6
by: Daniel Walzenbach | last post by:
Hi, I have a web application which sometimes throws an “out of memory” exception. To get an idea what happens I traced some values using performance monitor and got the following values (for...
4
by: mirandacascade | last post by:
O/S : Win2K vsn of Python: 2.4 Hoping to find information that provide information about error messages being encountered. Pythonwin session: Traceback (most recent call last): File...
3
by: Ramashish Baranwal | last post by:
Hi, I want to use variables passed to a function in an inner defined function. Something like- def fun1(method=None): def fun2(): if not method: method = 'GET' print '%s: this is fun2' %...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.