完整的程序(第1部分有效):def AddSpaces(auth,numb):
print("Runs")
while len(auth) < numb:
auth = auth +""
return (auth)
menu = 1
while menu <= 2:
menu = int(input("1. Add a new bookn2. Search for a new book by a given authorn3. End"))
if menu ==1:
BOOKS = open("BOOKS.txt","a")
Author = str(input("Author:"))
if len(Author) < 16:
Author = AddSpaces(Author,16)
while len(Author) > 16:
Author = str(input("Author:"))
Author = AddSpaces(Author,16)
print(Author)
Book = str(input("Book:"))
if len(Book) < 20:
Book = AddSpaces(Book,20)
while len(Book) > 16:
Book = str(input("Book:"))
Book = AddSpaces(Book,20)
print(Book)
Price = str(input("Price:"))
while len(Price)>5:
Price = str(input("Price:"))
Copies = str(input("Copies:"))
while len(Copies)>2:
Copies = str(input("Copies:"))
line = Author + Book + Price + Copies +"n"
print(line)
BOOKS.write(line)
BOOKS.close()
elif menu == 2:
BOOKS = open("BOOKS.txt","r")
while True:
AuthorSearch = str(input("Author name for search:"))
if len(AuthorSearch) < 16:
AuthorSearch = AddSpaces(AuthorSearch,16)
while len(AuthorSearch) > 16:
AuthorSearch = str(input("Author name for search:"))
AuthorSearch = AddSpaces(AuthorSearch,16)
print(AuthorSearch)
n = BOOKS.read()
if n =="":
print("End of file, no books found")
break
if AuthorSearch == line[:16]:
print ("Author found.")
print ("Author name:") + (line[:16])
print ("Book name:") + (line[17:36])
print ("Price:") + (line[37:41])
print ("Copies:") + (line[42:43])