Help with a function!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Traclo
    New Member
    • Oct 2007
    • 12

    Help with a function!

    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.

    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
    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.
    Code:
            if e[-1] != check_sum:
                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!
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Well, how 'bout:[CODE=python]if line[-1] != check_sum:
    print line[/CODE]I can't see where you're getting e from at the moment.

    Comment

    • Traclo
      New Member
      • Oct 2007
      • 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 :(

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Checksums are usually calculated by oring the previous result"[CODE=python]
        >>> 8 ^ 4
        12
        >>> [/CODE]

        Originally posted by Traclo
        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:[CODE=python]spudofile = [\
        '0-00-639498-1',
        '0-00-639498-2',
        '0-00-639498-3',
        '0-13-117655-2',
        '0-13-117655-3',
        '0-13-117655-4']



        def verify_isbn_fil e():
        ## file_name = pick_a_file()
        ## input_file = open(file_name)

        for line in spudofile:
        initial_multipl ier = 10
        check_sum = 0
        raw_number = line[0:-2].replace('-',' ')
        print raw_number

        for character in raw_number:
        if character != ' ':
        int_character = int(character)
        new_value = int_character*i nitial_multipli er
        check_sum += new_value
        initial_multipl ier -= 1

        print check_sum
        check_sum = check_sum%11
        print check_sum
        check_sum = 11-check_sum
        print check_sum
        check_sum = check_sum%11
        print check_sum
        check_sum = str(check_sum)
        print "______________ __"[/CODE]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
        _______________ _
        Last edited by bartonc; Oct 14 '07, 06:20 AM.

        Comment

        • Traclo
          New Member
          • Oct 2007
          • 12

          #5
          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...

          Comment

          • Traclo
            New Member
            • Oct 2007
            • 12

            #6
            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)

            Comment

            Working...