[Tutor] Checksum program

Wayne Werner waynejwerner at gmail.com
Wed Mar 23 23:43:32 CET 2011

On Wed, Mar 23, 2011 at 5:19 PM, Wayne Werner <waynejwerner at gmail.com>wrote:
> ---------- Forwarded message ----------
> From: Lezlie Kline <lezlie.kline at gmail.com>
> Date: Wed, Mar 23, 2011 at 9:30 AM
> Subject: Re: [Tutor] Checksum program
> To: Wayne Werner <waynejwerner at gmail.com>
> Wayne,
> Thanks!  That helped tremendously.  Here's my problem with the accumulator
> understanding.  I have two examples:
> This one is for an empty string:
> output=""
> print"Please enter 4 words, one at a time."
> for i in range(4):
>     word=raw_input("Enter a word: ")
>     output=output+" " + word
>     print "\tThe accumulator so far has: ", output
> print "The words you entered are: ", output
> The other one is for a non-empty string:
> output="*"
> word=raw_input("Enter a word: ")
> for letter in word:
>     output=output+letter+"*"
> print"The result is: ", output
> These are the two that I was going off of
> I'm not really getting an understanding of how this is working.  Do you
> mind explaining?  Is my ord(message[i]) correct for getting the ASCII
> numbers in the string?

Yes, that part works fine. If you fire up an interactive interpreter you can
try it out (I use IPython, so my prompt looks a little different than

In [12]: string = 'ASCII set'

In [13]: for i in xrange(len(string)):
   ....:     print ord(string[i])

You'll see that the output are indeed numbers.

> My thinking was that "message[i]" is the length so ord(value) is what
> provides the ASCII numbers therefore if I use ord(message[i]) that would
> work.  My problem was that I didn't know which of the two patterns above to
> follow because I don't really understand how they work.

The best way to learn how something works is to modify it, or try to break
it. Take the first example. What happens if you put more than one word in at
a time? What if you do numbers instead? What happens if you wrap int()
around the raw_input, so you have int(raw_input('Enter a word: ')) ? When
you get errors, read them! Python is one of the few languages that gives you
very helpful error messages (usually). Also, make sure you post the full
text of any and all errors here. Also it helps to explain

1) What were you doing? (And what were you trying to do? Sometimes these are
two different things!)

2) What did you expect to happen?

3) What happened instead?

Copying and pasting is a very good way to answer #3.

For instance, say you had this code:

In [14]: number = 'four'

In [15]: int(number)
ValueError                                Traceback (most recent call last)

/home/wayne/<ipython console> in <module>()

ValueError: invalid literal for int() with base 10: 'four'

You could say the following:

I was trying to turn a string into a number, but it didn't work. I got this
error instead:
<paste error here>

In this case, Python tells you with the last line that there's something
wrong with the value (hence ValueError). Of course the latter part of the
message might seem cryptic if you're not familiar with bases... But if you
got that error and were confused, then you could have sent an email to the
list and it would get answered fairly quickly. Of course, if you copy/paste
the last line into Google, you also tend to get some good results.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110323/3c62964e/attachment-0001.html>

More information about the Tutor mailing list