【发布时间】:2015-07-26 13:38:57
【问题描述】:
while True:
n=float(raw_input('Please enter your ID '))
if n in range (1000,10000):
break
print 'ID is incorrect, please try again'
with open("txt.txt") as searchfile: #searching for book
found = False
while not found:
b=str(raw_input('please enter a book '))
if b == '':
break # allow the search-loop to quit on no input
for line in searchfile:
if b in line:
print line
found = True
break
else:
print 'Please try again'
searchfile.seek(0) # reset file to the beginning for next search
class books:
'''class that supplys books to those who 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 from searching for book
book_1 = books(lis[0], lis[1], lis[2], lis[3])
book_1.description()
book_1.is_avaliable()
我有这个代码,第一部分工作正常,但我试图得到它,以便我的第一部分的结果,关于搜索书籍部分,打印输出作为列表。我试图在我以后的代码中访问该列表,然后在类部分中使用它。到目前为止,它读取并返回:
TypeError: 'file' 对象没有属性 'getitem'
【问题讨论】: