0

Hi !
I have books.txt and it contain 20 books with author,the books.txt look like so..
Mark Lutz/learning python
James payane/Beginning Python
Noah gift/Python for Unix and Linux System Administration

If user search for book it just print book and author if user search for author print (“you cannot search for author here ,you should look for title books here!.”)

def main():
    search = input("which titel of book you are looking after? ")
    if search in open("books.txt").read():
        parts = search.split("/")
        for part in parts:
            print(parts, " Books found.")



main()

Edited by tony75

2
Contributors
3
Replies
6
Views
4 Years
Discussion Span
Last Post by pyTony
0

You are not saving the books file which you read as one string at line 3 anywhere. Why are you splitting then input from user for title from '/'? Your example seems not allow '/' in title as it separates author from title.

0

Thanks Tony
If I change may question!

if user search for a book title or author and found was True it will show like this
Mark Lutz/learning python
if not tittle of book not found

def main():

    search = input("which titel of book you are looking for? ")
    file = open("books.txt")

    if search in open("books.txt").read():
        print(search, "found")

main()

which titel of book you are looking for? learning python
learning python found

which titel of book you are looking for? Mark Lutz
Mark Lutz found

But I neaad the out put will be(book title and author)
Mark Lutz/learning python

Edited by tony75

0

But I am not sure user can be able to give exactly same format, maybe only Family name of author, then wouldn't it be actually more usefull to show what there is in the line matching instead of echoing back the search string? So you should iterate the file line by line for matches.

Maybe you could compare your code with my example data file code snipet? http://www.daniweb.com/software-development/python/code/293490/text-file-based-information-access-by-field-name-and-number

Edited by pyTony

This article has been dead for over six months. Start a new discussion instead.
Take the time to help us to help you. Please be thoughtful and detailed and be sure to adhere to our posting rules.