Tell us what’s happening:
I am struggling to understand the necessity of the “given_check_digit” variable. The way I understand it, the user inputs the length after the commit, which then is either 10 or 13. This length has nothing to do with the check digit in my opinion.
I am now at the point where I have to debug test 11 but I don’t understand why the input should be invalid.
The variable expected_check_digit = 1, so between 0-9 and the length is 10.
Why should the input be invalid?
The input test 11 test:
Your code so far
def validate_isbn(isbn, length):
main_digits = isbn[0:length]
given_check_digit = int(isbn[len(main_digits)+1:])
if len(main_digits) != length:
raise ValueError(f'ISBN-{length} code should be {length} digits long.')
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 == 10 and (expected_check_digit == "X" or int(expected_check_digit) in range(10))) or (given_check_digit == 13 and int(expected_check_digit) in range(10)):
print('Valid ISBN Code.')
else:
raise ValueError('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 input_is_comma_separated(input):
if input.count(",") == 1:
return True
else:
return False
def isbn_is_digit(user_input):
if user_input.isdigit():
return True
else:
return False
def length_is_digit(length):
if length.isdigit():
return True
else:
return False
def validate_input(user_input):
if not input_is_comma_separated(user_input):
raise ValueError("Enter comma-separated values.")
else:
values = user_input.split(",")
if not length_is_digit(values[1]):
raise TypeError("Length must be a number.")
elif not isbn_is_digit(values[0]):
raise TypeError("Invalid character was found.")
def main():
user_input = input('Enter ISBN and length: ')
try:
validate_input(user_input)
except ValueError as e:
print(f"{e}")
except TypeError as e:
print(f"{e}")
values = user_input.split(",")
isbn = values[0]
length = int(values[1])
try:
validate_isbn(user_input, length)
except ValueError as e:
print(f"{e}")
except TypeError as e:
print(f"{e}")
#main()
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.3.1 Safari/605.1.15
Challenge Information:
Debug an ISBN Validator - Debug an ISBN Validator