471,342 Members | 1,994 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Help with a function!

12
I wrote this function for my introductory programming class in university. It is used to verify that an ISBN is valid. It does this by checking the calculated checksum of the number to the last digit of the number. It is supposed to do this for an entire txt file.

Expand|Select|Wrap|Line Numbers
  1. def verify_isbn_file():
  2.     file_name = pick_a_file()
  3.     input_file = open(file_name)
  4.  
  5.     for line in input_file:
  6.         initial_multiplier = 10
  7.         check_sum = 0
  8.         raw_number = line.replace('-',' ')
  9.  
  10.         for character in raw_number[0:-2]:
  11.             if character != ' ':
  12.                 int_character = int(character)
  13.                 new_value = int_character*initial_multiplier
  14.                 check_sum += new_value
  15.                 initial_multiplier -= 1
  16.  
  17.         check_sum = check_sum%11
  18.         check_sum = 11-check_sum
  19.         check_sum = check_sum%11
  20.         check_sum = str(check_sum)
  21.  
  22.         if check_sum == '10':
  23.             check_sum = 'x'
  24.         if e[-1] != check_sum:
  25.             print line
The only thing is it doesn't work. I've checked all my values and know that the embedded functions work. Also the algorithm works and returns a check sum that is the same as the last number of the line. So essentially all of what I wrote works but one part.
Expand|Select|Wrap|Line Numbers
  1.         if e[-1] != check_sum:
  2.             print e
Unfortunately this part is what screws up. e[-1] refers for some reason to the very last element of the ENTIRE file, as opposed to each line of the loop. So then only the last number of the file is checked against the check sum.
What I was wondering was if someone would be so kind as to tell me if there is some way to refer to the last character of each line in the txt file rather then the last character in the entire file?

Example txt file containing valid ISBN and invalid ISBN:
  • 0-00-639498-1
    0-00-639498-2
    0-00-639498-3
    0-13-117655-2
    0-13-117655-3
    0-13-117655-4

Thanks in advance!
Oct 14 '07 #1
5 1164
bartonc
6,596 Expert 4TB
Well, how 'bout:
Expand|Select|Wrap|Line Numbers
  1. if line[-1] != check_sum:
  2.     print line
I can't see where you're getting e from at the moment.
Oct 14 '07 #2
Traclo
12
My apologies! That was a remnant of previous version of the code. I have corrected e to line. But unfortunately that isn't the source of the error :(
Oct 14 '07 #3
bartonc
6,596 Expert 4TB
Checksums are usually calculated by oring the previous result"
Expand|Select|Wrap|Line Numbers
  1. >>> 8 ^ 4
  2. 12
  3. >>> 
My apologies! That was a remnant of previous version of the code. I have corrected e to line. But unfortunately that isn't the source of the error :(
Looks like an algorithm problem to me. Maybe this will give you some insite:
Expand|Select|Wrap|Line Numbers
  1. spudofile = [\
  2. '0-00-639498-1',
  3. '0-00-639498-2',
  4. '0-00-639498-3',
  5. '0-13-117655-2',
  6. '0-13-117655-3',
  7. '0-13-117655-4']
  8.  
  9.  
  10.  
  11. def verify_isbn_file():
  12. ##    file_name = pick_a_file()
  13. ##    input_file = open(file_name)
  14.  
  15.     for line in spudofile:
  16.         initial_multiplier = 10
  17.         check_sum = 0
  18.         raw_number = line[0:-2].replace('-',' ')
  19.         print raw_number
  20.  
  21.         for character in raw_number:
  22.             if character != ' ':
  23.                 int_character = int(character)
  24.                 new_value = int_character*initial_multiplier
  25.                 check_sum += new_value
  26.                 initial_multiplier -= 1
  27.  
  28.         print check_sum
  29.         check_sum = check_sum%11
  30.         print check_sum
  31.         check_sum = 11-check_sum
  32.         print check_sum
  33.         check_sum = check_sum%11
  34.         print check_sum
  35.         check_sum = str(check_sum)
  36.         print "________________"
0 00 639498
164
10
1
1
________________
0 00 639498
164
10
1
1
________________
0 00 639498
164
10
1
1
________________
0 13 117655
130
9
2
2
________________
0 13 117655
130
9
2
2
________________
0 13 117655
130
9
2
2
________________
Oct 14 '07 #4
Traclo
12
Embarrassing! So many tiny flaws in my programming, but I guess that's why I'm still learning. Again I've corrected the mistake in my first post by making raw_number refer to line[0:-1]
But I'd say that isn't the problem. Because if it were working correctly then it shouldn't have printed any numbers for "print line" because none of the last numbers were equal to the check sum!

So again for some reason the line[-1] seems to be referring only to the last character in the file. :(
Maybe I have other errors.

And thank you for the help!! It's very much appreciated, and I'm sure you remember being a beginner ;)

One thing I thought of trying to see what was going on was to put:
print line[-1] just before the end of the loop to see if it was a real value. What it ended up doing was printing nothing all loop long untill the final iteration, where it printed that last character...
Oct 14 '07 #5
Traclo
12
Solved! It was because list[-1] was actually \n because it was going to a new line.
so I put list.strip()[-1] and life got 10x better.

Thank you for the help (helped me fix some errors :D)
Oct 14 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by Alan Bashy | last post: by
2 posts views Thread by Jackson Yap | last post: by
5 posts views Thread by Craig Keightley | last post: by
1 post views Thread by Michael D. Reed | last post: by
1 post views Thread by Rahul | last post: by
4 posts views Thread by Stef Mientki | last post: by
22 posts views Thread by Amali | last post: by

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.