So, I'm trying to take information from bookfile.txt and display information from it. So far, I've opened the file. I'm having trouble getting each line as one object of arrayBooks.
I'd like to have arrayBooks[0]="Animal Farm,1945,152,George Orwell"
Then, I'm going to use book1 = arrayBooks[0].split(',') to split it to other information, such as:
book1[0]="Animal Farm"
book1[1]=1945
book1[2]=152
book1[3]="George Orwell"
So, when I want to find the shortest book, I can compare book1[2] to book2[2] and book3[2] to do so.
My main problem is getting the information in the array and usable. Anything I've tried doesn't seem to work and gives an error in the displayAll() function.
I'm using the displayAll() as a control because I feel if I can get the information to display, I will have it to use.
bookfile.txt:
Animal Farm,1945,152,George Orwell
To Kill A Mockingbird,1960,324,Harper Lee
Pride and Prejudice,1813,279,Jane Austen and Anna Quindlen
def main():
print("Welcome!")
displayBookMenu()
populateBooks()
displayAll(populateBooks())
def displayBookMenu:
print("\n1: Display All Books")
print("2: Display Shortest Book")
print("3: Display Longest Book")
print("3: Display Oldest Book")
print("4: Display Newest Book")
print("0: End")
choice = int(input("Choice: "))
if choice == 1:
displayAll()
elif choice == 2:
displayShortest()
elif choice == 3:
displayLongest()
elif choice == 4:
displayOldest()
elif choice == 5:
displayNewest()
elif choice == 0:
exit()
else:
print("Invalid Input")
def populateBooks():
fp = open("bookfile.txt", "r")
return fp.readlines()
def displayAll(arrayBooks):
print ("\nAll Books: \n")
#THIS IS WHERE I GET ERROR vvv
print arrayBooks[0]
def displayShortest():
def displayLongest():
def displayOldest():
def displayNewest():
main()