问题
I am making an ISBN program to solve the check digit and I want to make it so that when the program finds the check digit for you it opens a new string saying "would you like to close the program or not", which I have already done so.
If they person says 'n' for no it returns back to the beginning and if the person says 'y' the program closes I got stuck and started to search the internet my code is below could anyone help adjust it thanks you.
HERES MY CODE:
ISBN=input("Please enter a 10 digit number for the ISBN check digit: ")
while len(ISBN)!= 10:
print("Please try again and make sure you entered 10 digits.")
ISBN=int(input("Please enter the 10 digit number again: "))
continue
else:
D1 =int(ISBN[0])*11
D2 =int(ISBN[1])*10
D3 =int(ISBN[2])*9
D4 =int(ISBN[3])*8
D5 =int(ISBN[4])*7
D6 =int(ISBN[5])*6
D7 =int(ISBN[6])*5
D8 =int(ISBN[7])*4
D9 =int(ISBN[8])*3
D10=int(ISBN[9])*2
Sum=(D1+D2+D3+D4+D5+D6+D7+D8+D9+D10)
Mod=Sum%11
D11=11-Mod
if D11==10:
D11='X'
ISBNNumber=str(ISBN)+str(D11)
print("Your 11 digit ISBN Number is *" + ISBNNumber + "*")
def close():
close=input ("would you like to close the program or try again 'y' for Yes and 'n' for No:")
while len(close)==1:
if input == "n":s
return (ISBN)
elif input == "y":
exit()
close()#
回答1:
The easiest way is to wrap everything in a while
loop:
while True:
# ... put all your code here
close = input("Would you like to try again? Enter 'y' for Yes and 'n' for No: ")
if close.lower() in ("n", "no"):
print("Exiting")
break
This will loop each time, unless the user enters 'n'
(or similar). Note:
- Clearer question, with an explicit yes or no answer; and
- Use of
lower
andin
to allow range of possible valid inputs.
More broadly, I think you have problems with your algorithm; the 10th character (check digit) for an ISBN-10 number is calculated based on the first nine: http://en.wikipedia.org/wiki/Check_digit#ISBN_10.
回答2:
This adds a for loop in to your code
else:
Sum = 0
for i in range(len(isbn)):
sum= int(isbn[i])
mod=sum%11
digit11=11-mod
if digit11==10:
digit11='X'
iSBNNumber=str(isbn)+str(digit11)
print('Your 11 digit ISBN Number is ' + iSBNNumber)
来源:https://stackoverflow.com/questions/21309406/isbn-check-digit-solver-user-feedback