[Tutor] Checksum program
michael scott
jigenbakuda at yahoo.com
Wed Mar 23 15:29:03 CET 2011
Hi Lezlie,
Well first off, let me admit I have no idea what checksums are (I be a noobz)
and I can not help you with that part of your program at all, but there are lots
of people here who can. But since you are new to python, let me comment on some
of the general things I noticed in your code.
When I ran your code with the word being hello I got the error. "TypeError:
cannot concatenate 'str' and 'int' objects" it occured at line 8 of your code
"output=output+name+ord(message[i])". Now in this line you are adding 2 strings
together (output and name) with 1 int (ord(message[i])). In python you can not
add these types together. Hence the error. But if it did add them together
(using hello as an example) you would get output = hellohello104, is this what
you want? Since message = hello and output = hello and ord("h") = 104.
Also keep in mind that for every iteration of the for loop, output actually
changes (meaning that output lost its last value, to make room for the new one,
and it only has 1 value in it), so if python actually did what you asked it to
your output would look like this.
The value of message[i] is h
The value of the message is hellohello104
The value of message[i] is e
The value of the message is hellohello101
The value of message[i] is l
The value of the message is hellohello108
The value of message[i] is l
The value of the message is hellohello108
The value of message[i] is o
The value of the message is hellohello111
The checksum is , 111 (according to "checksum=(output)%256" supposing the
hellohello was not added and just 111 was evaluated, if you had put in
"256%(output)" the answer would have been 54 instead of 111)
Note that because it changed in every iteration the last letter (o) is the
final value of output, instead of the values being appended to the variable
output
Is this what you wanted to happen? If its not, try to think of ways to slighty
change your code to give you closer results to what you want. If this is what
you want, there are cheats to actually make it output this without errors...
I hope this helps (^_^)
----
What is it about you... that intrigues me so?
________________________________
From: Lezlie Kline <lezlie.kline at gmail.com>
To: tutor at python.org
Sent: Wed, March 23, 2011 9:09:53 AM
Subject: [Tutor] Checksum program
Hi,
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.
def main():
print"This program creates a checksum for a message."
name=raw_input("Please enter the message to encode: ")
message=name
output=name
for i in range(len(message)):
print"The value of message[i] is ", message[i]
output=output+name+ord(message[i])
print"The value of the message is ", output
checksum=(output)%256
print"The checksum is ", checksum
main()
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
I'd appreciate any help offered as I'm "pulling out my hair."
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110323/bc5964c8/attachment.html>
More information about the Tutor
mailing list