python - Trying to get a return statement to be formatted as list and then used by a class and then run through if statements -
while true: n=float(raw_input('please enter id ')) if n in range (1000,10000): break print 'id incorrect, please try again' open("txt.txt") searchfile: #searching book found = false while not found: b=str(raw_input('please enter book ')) if b == '': break # allow search-loop quit on no input line in searchfile: if b in line: print line found = true break else: print 'please try again' searchfile.seek(0) # reset file beginning next search class books: '''class supplys books need them most''' def __init__(self,id_number,book_title, author,avaliable): self.id_number=id_number self.book_title=book_title self.author=author self.avaliable=avaliable def description(self): print '%s %s %s %s'% (self.id_number,self.book_title,self.author,self.avaliable) def __getitem__(self,i): return self.availiable[i] print searchfile[3] def is_avaliable(self): if self.avaliable==0: print 'taken' else: print 'you can have me' lis = searchfile #using result searching book book_1 = books(lis[0], lis[1], lis[2], lis[3]) book_1.description() book_1.is_avaliable()
i have code, first section working fine, trying result of first section, regarding searching book section, printed output list.i trying access list in later code , use in class section. far reads through , returns this:
typeerror: 'file' object has no attribute 'getitem'
your lis = searchfile
line assigns (now closed) file descriptor searchfile
lis
try access indices it. i'm assuming want instead is:
def search(): open('txt.txt') searchfile: while true: b = str(raw_input('please enter book ')) if b == '': break # allow search-loop quit on no input line in searchfile: if b in line: return line # return line here!! else: print 'please try again' searchfile.seek(0) # reset file beginning next search return none lis = search() # lis line book found @ # rest of code written ...
Comments
Post a Comment