[Tutor] Fwd: Checksum program
waynejwerner at gmail.com
Wed Mar 23 23:19:30 CET 2011
Forwarding on to the list, as there are many other people who can also
answer the questions.
---------- 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>
Thanks! That helped tremendously. Here's my problem with the accumulator
understanding. I have two examples:
This one is for an empty string:
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:
word=raw_input("Enter a word: ")
for letter in word:
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? 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.
Any further help is truly appreciated. As you might perceive, I'm a bit
frazzled. Thanks so much.
On Wed, Mar 23, 2011 at 9:50 AM, Wayne Werner <waynejwerner at gmail.com>wrote:
> On Wed, Mar 23, 2011 at 8:09 AM, Lezlie Kline <lezlie.kline at gmail.com>wrote:
>> I'm trying to work out the bugs in a program for calculating the checksum
>> (modulo 256) of an input string. I'm testing it with my full name and I'm a
>> beginner with Python. Here's what I have so far.
> Welcome to the list and to Python!
>> def main():
>> print"This program creates a checksum for a message."
>> name=raw_input("Please enter the message to encode: ")
>> for i in range(len(message)):
> Using the range in this case is superfluous twice; Range is a function that
> returns a list - in this case a list containing 0-len(message)-1. xrange is
> the preferred function, which creates an generator instead. It uses a lot
> less memory. But in *this* case, the only thing you're using i for is to
> index a string - which is already iterable. You could say:
> for letter in message:
> print "The letter is: ", letter
>> print"The value of message[i] is ", message[i]
>> print"The value of the message is ", output
>> print"The checksum is ", checksum
>> I know I'm offbase somewhere, but I'm not understanding some parts of the
>> accumulator part of the program. I need it to work with the message[i]
>> intact. In other words, I need the pseudo code to go something like this:
>> print message
>> get input
>> find length
>> using length in range function accumulate ASCII numbers
>> calculate checksum
>> print checksum
> Actually, you could even improve your pseudo code - rather than describing
> *what* you need to do, you've described *how* you need to do it - the how
> should always be your last step, even if you're doing it just in your head.
> So in this case, you could do something like this:
> get a string
> display string
> get ASCII numbers for each character in the string
> add all the ascii values together
> calculate checksum
> display checksum
> So let's take a look at your function to see which steps you've got, and
> where you're missing:
> def main():
> print"This program creates a checksum for a message."
> # Okay, here you're getting a string. Looks good so far
> name=raw_input("Please enter the message to encode: ")
> # I'm not really sure what the purpose of this reassignment is.
> # Your pseudo code doesn't mention anything that would need these
> copies for anything
> for i in range(len(message)):
> print"The value of message[i] is ", message[i]
> # What is stored in output the first loop through? How about name?
> # If this is where you *think* you're accumulating ASCII digits,
> you're definitely not.
> print"The value of the message is ", output
> # This certainly does %256, but it requires a number.
> print"The checksum is ", checksum
> So really you're only missing the most crucial step - totalling the ASCII
> Rather than "output", a more appropriate name might be "total", since
> that's what you're really trying to store.
> Good luck!
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor