0
class book:
    def __init__(self):
        print("class created")
    def input(self):
        no_list=[]
        fp="book_file.txt"
        file=open(fp,"r")
        x_list=[]
        y_list=[]
        while True:
            x=(file.readline()).strip("\n")
            x_list.append(x)
            y=file.readline()
            y_list.append(y)
            #z=file.readline()
        print(x_list)
        print(y_list)

b=book()
b.input()

The first three lines of the text file have the number of books, title, and price. After retrieving the values in variables I have to find the cost in method of the class book

  • Please reformat this, and details of bookfile.txt would be helpful too. – DrCorgi Jul 31 '22 at 02:42
  • 2
    Please also describe what is the issue or what are the errors that you are getting with your code. If you are getting error messages, please [edit] to post that too. – Gino Mempin Jul 31 '22 at 02:51
  • 1
    Though I would say, you don't need to store each line in separate variables or in separate lists. Store _all_ the lines in 1 list, and just index them: [How to read a file line-by-line into a list?](https://stackoverflow.com/q/3277503/2745495) – Gino Mempin Jul 31 '22 at 02:53

1 Answers1

0

"from linecache import getline" allows you to get specified lines according to indexs

official docs about it:https://docs.python.org/3/library/linecache.html

bz_jf
  • 11
  • 4