Debug an ISBN Validator - Debug an ISBN Validator

Tell us what’s happening:

Hello, I am wondering why step 7 is not completing below. Also, when inputting “1530051125,A” the code just seems to continuously run. Any ideas why? It seems like steps 7 and 8 should function the same to me. However, step 8 works as expected but step 7 is giving me issues.

My code modifications are under the main() function.

Thank you.

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]
    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: ')
    if "," not in user_input:
        print("Enter comma-separated values.")
        return
    values = user_input.split(',')
    
    isbn = values[0]
    if not isbn:
        print("Invalid character was found.")
        return
    elif any(c.isalpha() for c in isbn):
        print("Invalid character was found.")
        return
    else: 
        int(isbn)
        return

    length = values[1]
    if not length:
        print("Length must be a number.")
        return
    elif any(c.isalpha() for c in length):
        print("Length must be a number.")
        return
    else: 
        int(length)
        return
    
    if length == 10 or length == 13:
        validate_isbn(isbn, length)
        return
    else:
        print('Length should be 10 or 13.')
        return
        
    

main()

Your browser information:

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

Challenge Information:

Debug an ISBN Validator - Debug an ISBN Validator

Hi @codecampacct

Did you see the hint message?

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

You need to check if the input is in a valid format.

Happy coding

Hello, thank you for the response. I did not comment out the main() function so that I can test out the code by entering an input in the below screenshot. I did try commenting out the main function but my code for 7 did not pass.

I believe the main function is taking the input function which turns the input into a string. My if conditions are intended to check if the input is blank or contains an alpha character. If not blank or not containing an alpha, then the values are converted into an integer.

Step 8 is passing for me which I am now actually thinking is incorrect because the ISBN number should be able to take in an X per the instructions.

I was not really sure why my step 7 is not passing though because the if condition for step 7 seems like it should be the same as step 8 in regards to checking if the value has any non-numeric values.

if you have issues following the code logic you can use a tool like https://pythontutor.com/ that shows what your code does line by line, you will want to run the code with the main() call present, and when you are asked for an input, one of tose that are giving you issues

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.