我有一个用于验证ISBN 10的功能。如果输入来自用户,它的效果非常好。这还不够,我想从文本文件输入并将输出存储在文本文件中。
print("ISBN-Validation Program")
isbn = input("Enter isbn: ")
def stripDashes(isbn):
sisbn = isbn.replace("-", "").replace(" ", "").upper();
return checkFormat(sisbn)
def checkFormat(isbn):
if len(isbn) == 10 and isbn[:9].isdigit()\
and (isbn[-1] == "x" or isbn[-1] == "X" or isbn[-1].isdigit()):
return isValiDisbn(isbn)
else:
print("ISBN is not properly formatted.")
def isValiDisbn(isbn):
if (isbn[-1] == "x" or isbn[-1] == "X"):
total = int(isbn[0])*10 + int(isbn[1])*9 + int(isbn[2])*8\
+ int(isbn[3])*7 + int(isbn[4])*6 + int(isbn[5])*5\
+ int(isbn[6])*4 + int(isbn[7])*3 + int(isbn[8])*2 + 10
else:
total = int(isbn[0])*10 + int(isbn[1])*9 + int(isbn[2])*8\
+ int(isbn[3])*7 + int(isbn[4])*6 + int(isbn[5])*5\
+ int(isbn[6])*4 + int(isbn[7])*3 + int(isbn[8])*2 + int(isbn[9])
if total % 11 == 0:
print("The number is valid.")
else:
print("The number is not valid.")
stripDashes(isbn)
以上功能适用于用户输入,我想我的调用函数代码和文本文件中的打印在某处是错误的。
def main():
inFile = open("isbn.txt", "r")
outFile = open("isbnOut.txt", "a")
for line in open("isbn.txt", "r"):
isbns = line.split()
for isbn in isbns:
if checkFormat(isbn) == False:
outFile.write(isbn.strip()+"\nISBN is not properly formatted.\n")
if isValiDisbn(isbn) == True:
outFile.write(isbn.strip()+"\nThe number is valid.\n")
if isValiDisbn(isbn) == False:
outFile.write(isbn.strip()+"\nThe number is not valid.\n")
inFile.close()
outFile.close()
def stripDashes(isbn):
sisbn = isbn.replace("-", "").replace(" ", "").upper();
return checkFormat(sisbn)
def checkFormat(isbn):
if len(isbn) == 10 and isbn[:9].isdigit() and (isbn[-1] == "x" or isbn[-1] == "X" or isbn[-1].isdigit()) == True:
return isValiDisbn(isbn)
else:
return False
#print("ISBN is not properly formatted.")
def isValiDisbn(isbn):
if (isbn[-1] == "x" or isbn[-1] == "X"):
total = int(isbn[0])*10 + int(isbn[1])*9 + int(isbn[2])*8 + int(isbn[3])*7\
+ int(isbn[4])*6 + int(isbn[5])*5 + int(isbn[6])*4 + int(isbn[7])*3\
+ int(isbn[8])*2 + 10
else:
total = int(isbn[0])*10 + int(isbn[1])*9 + int(isbn[2])*8 + int(isbn[3])*7\
+ int(isbn[4])*6 + int(isbn[5])*5 + int(isbn[6])*4 + int(isbn[7])*3\
+ int(isbn[8])*2 + int(isbn[9])
if total % 11 == 0:
return True
#print("The number is valid.")
else:
return False
#print("The number is not valid.")
main()
有谁能告诉我这里有什么问题并帮助我解决这个问题?
答案 0 :(得分:0)
问题是您忘记拨打stripDashes
,因此ISBN以int
而非0-306-40615-2
0306406152
def stripDashes(isbn):
sisbn = isbn.replace("-", "").replace(" ", "").upper();
return sisbn
作为循环的一部分,请执行:
def main():
inFile = open("isbn.txt", "r")
outFile = open("isbnOut.txt", "a")
for line in open("isbn.txt", "r"):
isbns = line.split()
for isbn in isbns:
stripped_isbn = stripDashes(isbn)
if checkFormat(stripped_isbn) == False:
outFile.write(isbn.strip()+"\nISBN is not properly formatted.\n")
if isValiDisbn(stripped_isbn) == True:
outFile.write(isbn.strip()+"\nThe number is valid.\n")
if isValiDisbn(stripped_isbn) == False:
outFile.write(isbn.strip()+"\nThe number is not valid.\n")
工作正常:
$ cat isbn.txt
0-306-40615-2
$ python isbn.py
$ cat isbnOut.txt
0306406152
The number is valid.
其他注意事项:
inFile = open("isbn.txt", "r")
,但随后在循环for line in open("isbn.txt", "r"):
中重新打开。只需使用您的打开文件,例如for line in inFile:
或删除第一个开头行。执行文件外写入的代码已损坏。
if checkFormat(isbn) == False:
outFile.write(isbn.strip()+"\nISBN is not properly formatted.\n")
if isValiDisbn(isbn) == True:
outFile.write(isbn.strip()+"\nThe number is valid.\n")
if isValiDisbn(isbn) == False:
outFile.write(isbn.strip()+"\nThe number is not valid.\n")
如果checkFormat
返回False
,它仍会检查ISBN的有效性,因此elif
应用于以下行。还需要isValiDisbn(isbn) == False
,因为它会调用isValiDisbn
两次,并将其与False
进行比较(只需使用not
),因此只需使用else
:< / p>
if checkFormat(isbn) == False:
outFile.write(isbn.strip()+"\nISBN is not properly formatted.\n")
elif isValiDisbn(isbn) == True:
outFile.write(isbn.strip()+"\nThe number is valid.\n")
else:
outFile.write(isbn.strip()+"\nThe number is not valid.\n")