31.2k views
2 votes
NEEDS TO BE IN PYTHON:ISBN-13 is a new standard for indentifying books. It uses 13 digits d1d2d3d4d5d6d7d8d910d11d12d13 . The last digit d13 is a checksum, which is calculated from the other digits using the following formula:10 - (d1 + 3*d2 + d3 + 3*d4 + d5 + 3*d6 + d7 + 3*d8 + d9 + 3*d10 + d11 + 3*d12) % 10If the checksum is 10, replace it with 0. Your program should read the input as a string. Display "incorrect input" if the input is incorrect.Sample Run 1Enter the first 12 digits of an ISBN-13 as a string: 978013213080The ISBN-13 number is 9780132130806Sample Run 2Enter the first 12 digits of an ISBN-13 as a string: 978013213079The ISBN-13 number is 9780132130790

User Npskirk
by
5.3k points

1 Answer

2 votes

Answer:

Follows are the code to this question:

n=input("Enter the first 12 digits of an ISBN-13 as a string:")#defining a varaible isbn for input value

if len(n)!=12: #use if block to check input value is equal to 12 digits

print("incorrect input") #print error message

elif n.isdigit()==False: #use else if that check input is equal to digit

print("incorrect input") #print error message

else:# defining else block

s=0 #defining integer vaiable s to 0

for i in range(12):#defining for loop to calculate sum of digit

if i%2==0: #defining if block to check even value

s=s+int(n[i])#add even numbers in s vaiable

else: #use else block for odd numbers

s=s+int(n[i])*3 #multiply the digit with 3 and add into s vaiable

s=s%10#calculate the remainder value

s=10-s#subtract the remainder value with 10 and hold its value

if s==10: #use if to check s variable value equal to 10

s=0#use s variable to assign the value 0

n=n+s.__str__() #u

Output:

please find attached file.

Step-by-step explanation:In the above Python code, the "n" variable is used for input the number into the string format uses multiple conditional statements for a check input value, which can be defined as follows:

  • In if block, it checks the length isn't equal to 12, if the condition true, it will print an error message.
  • In the else, if the block it checks input value does not digit, if the condition is true, it will print an error message.
  • In the else block, it uses the for loop, in which it calculates the even and odd number sum, and in the odd number, we multiply by 3 then add into s variable.
  • In this, the s variable is used to calculate its remainder and subtract from the value and use the if block to check, its value is not equal to 10 if it's true, it adds 0 into the last of n variable, otherwise, it adds its calculated value.
NEEDS TO BE IN PYTHON:ISBN-13 is a new standard for indentifying books. It uses 13 digits-example-1
User Allnodcoms
by
5.1k points