Tell us what’s happening:
This has been a two whole day venture where I’m stuck on the same issue.
I am having trouble with 9, 10, 11 and likely so on; I’m trying to understand what is wrong with 9 right now as my tests come back correct in pythontutor – but I get a value error when i try to run main() in the lab terminal. i assume the problem lies in the main function.
thanks
Your code so far
def validate_isbn(isbn, length):
if len(str(isbn)) != length:
return(f'ISBN-{length} code should be {length} digits long.')
main_digits = isbn[:-1]
given_check_digit = isbn[-1]
main_digits_list = [int(digit) for digit in main_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)
if given_check_digit == expected_check_digit:
return('Valid ISBN Code.')
else:
return('Invalid ISBN Code.')
def calculate_check_digit_10(main_digits_list):
digits_sum = 0
for index, digit in enumerate(main_digits_list):
digits_sum += digit * (10 - index)
result = 11 - digits_sum % 11
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):
digits_sum = 0
for index, digit in enumerate(main_digits_list):
if index % 2 == 0:
digits_sum += digit * 1
else:
digits_sum += digit * 3
result = 10 - digits_sum % 10
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(',')
if ',' not in user_input:
print('Enter comma-separated values.')
return
isbn = values[0]
try:
isbn = int(isbn)
except ValueError:
print('Invalid character was found.')
try:
length = int(values[1])
except ValueError:
print('Length must be a number.')
if length == 10 or length == 13:
return validate_isbn(str(isbn), length)
else:
return('Length should be 10 or 13.')
(main())
#print(validate_isbn('1530051126',10))
#print(validate_isbn('1530051125',10))
#print(validate_isbn('9781530051120',10))
#print(validate_isbn(15-0051126,10))
#print(validate_isbn('1530051126',9))
#print(validate_isbn(153005115,A))
#print(validate_isbn(1530051125 5))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36
Challenge Information:
Debug an ISBN Validator - Debug an ISBN Validator
