473,248 Members | 1,437 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,248 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 1260
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

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

Similar topics

7
by: Alan Bashy | last post by:
Please, guys, In need help with this. It is due in the next week. Please, help me to implement the functions in this programm especially the first three constructor. I need them guys. Please, help...
2
by: Jackson Yap | last post by:
can someone kind enough to help me look at the attached html and js file? Why is it that the javascript menu could not work at www.apchosting.net but could work at...
5
by: Craig Keightley | last post by:
Please help, i have attached my page which worksin IE but i cannnot get the drop down menu to fucntion in firefox. Any one have any ideas why? Many Thanks Craig ...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
1
by: Michael D. Reed | last post by:
I am using the help class to display a simple help file. I generated the help file using Word and saving it as a single page Web page (.mht extension). I show the help file with the following...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
4
by: Stef Mientki | last post by:
I'm making special versions of existing functions, and now I want the help-text of the newly created function to exists of 1. an extra line from my new function 2. all the help text from the old...
22
by: Amali | last post by:
I'm newdie in c programming. this is my first project in programming. I have to write a program for a airline reservation. this is what i have done yet. but when it runs it shows the number of...
1
by: Bob | last post by:
Hi, Hope you can help me with this one. I'm at my wits end. I'm trying to create an intelligent edit-box like the excellent "Customer" one at the URL: ...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
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: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
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...

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.