【问题标题】:Trying to get a return statement to be formatted as list and then used by a class and then run through if statements试图将返回语句格式化为列表,然后由类使用,然后运行 ​​if 语句
【发布时间】: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'

【问题讨论】:

    标签: python class return


    【解决方案1】:

    您的lis = searchfile 行将(现已关闭的)文件描述符searchfile 分配给lis 然后您尝试从中访问索引。我假设你想要的是:

    def search():
        with open('txt.txt') as searchfile:
            while True:
                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:
                        return line  # return your line here!!
                else:
                    print 'Please try again'
                    searchfile.seek(0)  # reset file to the beginning for next search
            return None
    
    lis = search()  # lis is now the line that the book is found at
    # the rest of your code as written
    ... 
    

    【讨论】:

    • 这似乎让它运行在代码的顶部,但是当它低于'return none'时,它似乎并没有像我将类分开时那样运行在类中工作......在它打开返回值作为完整列表之前,现在它只打开输入的书的编号......
    • 我想我不确定你想做什么,那么?您发布的代码不会在任何地方创建列表。
    • 我试图做的是,一旦使用 return 函数访问了书籍详细信息,然后将该返回值保存为列表...使用保存的列表并访问每个元素,例如:说您有列表 '1,Book_1,Author_1,0' 您将能够使用代码访问列表的每个单独部分,然后如果第 3 部分为 0,那么它将返回书籍......如果这更有意义吗?
    猜你喜欢
    • 1970-01-01
    • 2018-03-28
    • 2011-11-19
    • 2018-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-06
    相关资源
    最近更新 更多