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.
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.
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:
Thanks in advance!
Code:
def verify_isbn_file(): file_name = pick_a_file() input_file = open(file_name) for line in input_file: initial_multiplier = 10 check_sum = 0 raw_number = line.replace('-',' ') for character in raw_number[0:-2]: if character != ' ': int_character = int(character) new_value = int_character*initial_multiplier check_sum += new_value initial_multiplier -= 1 check_sum = check_sum%11 check_sum = 11-check_sum check_sum = check_sum%11 check_sum = str(check_sum) if check_sum == '10': check_sum = 'x' if e[-1] != check_sum: print line
Code:
if e[-1] != check_sum: print e
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!
Comment