Debug an ISBN Validator - Debug an ISBN Validator

Tell us what’s happening:

Hello, when I type in the terminal : 1111111111,10 . It prints ‘Length should be 10 or 13.’ even though it is 10.

Your code so far

import re
def validate_isbn(isbn, length):
    if len(isbn, length) != length:
        print(f'ISBN-{length} code should be {length} digits long.')
        return
    main_digits = isbn[0:length]
    given_check_digit = isbn[length]
    main_digits_list = [int(digit) for digit in main_digits]
    # Calculate the check digit from other digits
    if length == 10:
        expected_check_digit = calculate_check_digit_10(main_digits_list)
    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.')
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)
    # Find the remainder of dividing the sum by 11, then subtract it from 11
    result = 11 - digits_sum % 11
    # 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'
    elif result == 10:
        expected_check_digit = 'X'
    else:
        expected_check_digit = str(result)
    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: ')
    try:
        values = user_input.split(',')
        isbn = values[0]
        length = values[1]
        if not re.fullmatch("\d+",isbn):
            print("Invalid character was found.")
            return
        else:
            pass
        if length == 10 or length == 13:
            validate_isbn(isbn, length)
        else:
            print('Length should be 10 or 13.')
    except IndexError:
        print("Enter comma-separated values.")
    except ValueError:
        print("Length must be a number.")
   
main()





Your browser information:

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

Challenge Information:

Debug an ISBN Validator - Debug an ISBN Validator

which test are you failing in the tested items on the left hand side of your window?

Test #1 specifically asks you to:

  1. You should comment out the call to the main function to allow for the rest of the tests to work properly.

currently you have the main() left open to be read by the Python interpreter. Comment that line out like we are instructed to and see if that may fix your problem. If not let me know and we will troubleshoot it further together.

Hi @LTr_08,

When I enter those values, I see this in the console:

Enter ISBN and length: 
1111111111,10
Invalid character was found.

So maybe the bit of code generating that message needs work.

Happy coding!

Hi, I commented out the main function and my doesn’t pass the tests in whichit should print “Valid/Invalid ISBN code” as well as the ones in which it should print “ISBN-10 code should be 10 digits long.” But it passes all other tests. And since these are print statements only found in the validate_isbn() function, it means that the main() function never gets to the point where it can execute the validate_isbn(). TL,DR it always shows an error message even though the input is good.

okay let me take a deeper look give me 5

I changed these lines:

isbn = int(values[0])
length = int(values[1])

But now it shows :

Enter ISBN and length: 
1111111111,10
Length must be a number.

So that means it stumbles upon a ValueError

Maybe there is a problem with this line ?

if not re.fullmatch("\d+",isbn):

comment out the #main() and put this underneath it

print(validate_isbn(1530051126,10))

your’e geting TypeErrors in the terminal. I agree with @dhess that some of the code in your blocks needs some touching up and fixing. start with what is causing that error. it is the code on line 3.

You only changed those two lines of code, right? Aren’t you seeing an error in the console?

Yeah for the moment I only changed these two and there is no error in the console

Yeah, I think I have to remove the length argument:

if len(isbn, length) != length:

Now I changed it:

if len(str(isbn)) != length:

len() can only take 1 argument by default. The error in the terminal I was referencing was in direct connection to this line of ccode

if len(isbn, length) != length:

This entire lesson is about debugging code you did not write. The user story states this verbatim:

Camperbot has tried to build their own ISBN validator. However, they have made a few mistakes along the way.

you are going to have to use the numbers they have provided and test them against the code and fix the errors along the way to pass the tests. Good Luck. Happy Coding!

there most likely will not be any errors in the console unless you run something through the code such as using print(validate_isbn(1530051126,10)) to test it and also having main() commented out with a #

So that means I have to leave the original lines of code untouched ? :sob:

just take a minute to slow down and read the user story from top to bottom before you write any more code partner.

Okay thanks I’m going to keep searching I’ll inform you if I find anything useful

I’m sorry. That’s incorrect. There can definitely be console errors while testing your code in the preview window.