Debug an ISBN Validator - Debug an ISBN Validator

Tell us what’s happening:

I afraid I don’t understand the initial inputs.

main_digits_list = [int(digit) for digit in main_digits]

I see others edit this line making it main_degits[-1]. I think this is removing the check digit from the calculation which results in a valid ISBN. The problem is that it also makes the second ISBN valid that is supposed to return as invalid. Since the only difference is the last digit: 1530051126(valid) vs 1530051125(invalid).

Without adding the -1 the check doesn’t ever return valid.

Your code so far

def validate_isbn(isbn, length):
    if len(isbn) != length:
        print(f'ISBN-{length} code should be {length} digits long.')
        return
    main_digits = isbn[0:length]
    print(f"main digits = {main_digits}")
    given_check_digit = isbn[length-1]
    print(f"given check digit = {given_check_digit}")
    main_digits_list = [int(digit) for digit in main_digits]
    #print(f"main digits list = {main_digits_list}")
    # Calculate the check digit from other digits
    if length == 10:
        expected_check_digit = calculate_check_digit_10(main_digits_list)
        print(f"validate isbn: expected check digit = {expected_check_digit}")
    else:
        expected_check_digit = calculate_check_digit_13(main_digits_list)
    # Check if the given check digit matches with the calculated check digit
    if given_check_digit == expected_check_digit:
        print('Valid ISBN Code.')
    else:
        print('Invalid ISBN Code.')
        print(f"check digit = {given_check_digit}, expected check digit returned: {expected_check_digit}")

def calculate_check_digit_10(main_digits_list):
    # Note: You don't have to fully understand the logic in this function.
    digits_sum = 0
    # Multiply each of the first 9 digits by its corresponding weight (10 to 2) and sum up the results
    for index, digit in enumerate(main_digits_list):
        digits_sum += digit * (10 - index)
        print(f"sum of digits = {digits_sum}")
    # Find the remainder of dividing the sum by 11, then subtract it from 11
    result = 11 - digits_sum % 11
    print(f"result of 11 - {digits_sum % 11} = {result}")
    # The calculation result can range from 1 to 11.
    # If the result is 11, use 0.
    # If the result is 10, use upper case X.
    # Use the value as it is for other numbers.
    if result == 11:
        expected_check_digit = '0'
        print(f"calc check digit: expected check digit = {expected_check_digit}")
    elif result == 10:
        expected_check_digit = 'X'
        #print(expected_check_digit)
    else:
        expected_check_digit = str(result)
        #print(expected_check_digit)
    return expected_check_digit

def calculate_check_digit_13(main_digits_list):
    # Note: You don't have to fully understand the logic in this function.
    digits_sum = 0
    # Multiply each of the first 12 digits by 1 and 3 alternately (starting with 1), and sum up the results
    for index, digit in enumerate(main_digits_list):
        if index % 2 == 0:
            digits_sum += digit * 1
        else:
            digits_sum += digit * 3
    # Find the remainder of dividing the sum by 10, then subtract it from 10
    result = 10 - digits_sum % 10
    # The calculation result can range from 1 to 10.
    # If the result is 10, use 0.
    # Use the value as it is for other numbers.
    if result == 10:
        expected_check_digit = '0'
    else:
        expected_check_digit = str(result)
    return expected_check_digit

def main():
    user_input = input('Enter ISBN and length: ')
    values = user_input.split(',')
    #print(values)
    isbn = values[0]
    #print(isbn)
    length = int(values[1])
    #print(length)
    if length == 10 or length == 13:
        validate_isbn(isbn, length)
    else:
        print('Length should be 10 or 13.')

main()

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/153.0.0.0 Safari/537.36

Challenge Information:

Debug an ISBN Validator - Debug an ISBN Validator

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-isbn-validator/686b9720ee1d032bd77a480a.md at main · freeCodeCamp/freeCodeCamp · GitHub

here, you copy the whole isbn as main_digits, that’s a bug you need to fix, the main digits do not contain the check digit

if you write

that is only the last digit, so the verification is not going to work

Hmm. I see.

I think a small explanation of what portions of the ISBN number represents would be rather helpful in place of “you don’t need to understand how this is calculated.” Or I’m just not smart enough to have gleaned it from code that doesn’t work.

Thanks! I was able to figure out what is supposed to be happening from your hints.