473,396 Members | 2,108 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,396 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 1265
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.