搜索库存并显示信息

时间:2019-03-01 13:20:56

标签: python inventory

我正在尝试编写一个搜索数据对象列表的代码,如果用户未输入参考号,则在用户输入有效参考号时应打印“找不到此类项目”,它应显示信息关于那个项目。按照我的代码现在的方式,它不会为每个数据对象打印找到这样的项目,而不会打印一次。如何使它只打印一次?

def initialize():
    medialist=[
    MediaItem("TU2RL012","Movie","2001: A Space Odyssey",11.99, None ,"Stanley Kubrick","Keir Dullea"),
    MediaItem("GV5N32M9","Book","A Brief History of Time",10.17,"Stephen Hawking", None, None),
    MediaItem("1DB6HK3L","Movie","North by Northwest",8.99, None, "Alfred Hitchcock","Cary Grant"),
    MediaItem("PO5T7Y89","Movie", "The Good, The Bad, The Ugly",9.99,None,"Sergio Leone", "Clint Eastwood"),
    MediaItem("TR3FL0EW","Book","The Alchemist",6.99,"Paulo Coelho", None,None),
    MediaItem("F2O9PIE9", "Book", "Thus Spoke Zarathustra",7.81, "Friedrich Nietzsche", None, None),
    MediaItem("R399CED1","Book", "Jonathan Living Seagull",6.97,"Richard Bach", None, None),
    MediaItem("2FG6B2N9","Movie", "Gone with the Wind",4.99, "Victor Fleming","Vivien Leigh", None),
    MediaItem("6Y9OPL87","Book", "Gone with the Wind",7.99, "Margarett Mitchell", None, None)]
    return medialist

def search_item():
    referencenum=input("Enter item reference:")
    for obj in initialize():
        if referencenum != obj.reference:
            print("No Such Object found")`

2 个答案:

答案 0 :(得分:1)

您可以首先在列表中收集所有对象参考编号

export function isLeapYear(year) {
  return (
    year % 4 === 0 && (year % 100 != 0 || year % 1000 === 0 || year % 400 === 0)
  )
}

export function isValidGermanDate(germanDate) {
  if (
    !germanDate ||
    germanDate.length < 5 ||
    germanDate.split('.').length < 3
  ) {
    return false
  }

  const day = parseInt(germanDate.split('.')[0])
  const month = parseInt(germanDate.split('.')[1])
  const year = parseInt(germanDate.split('.')[2])

  if (isNaN(month) || isNaN(day) || isNaN(year)) {
    return false
  }

  if (month < 1 || month > 12) {
    return false
  }

  if (day < 1 || day > 31) {
    return false
  }

  if ((month === 4 || month === 6 || month === 9 || month === 11) && day > 30) {
    return false
  }

  if (isLeapYear(year)) {
    if (month === 2 && day > 29) {
      return false
    }
  } else {
    if (month === 2 && day > 28) {
      return false
    }
  }

  return true
}

,然后检查是否匹配:

obj_references = [obj.reference for obj in initialize()]

要显示有关某个对象的更多信息,您可以先收集所有对象

def search_item():
    referencenum=input("Enter item reference:")
    obj_references = [obj.reference for obj in initialize()]
    if referencenum not in obj_references:
        print("No Such Object found")

然后,分别收集有关每个对象所需的信息

objects = initialize()

请注意,obj_references = [obj.reference for obj in objects] obj_titles = [obj.title for obj in objects] obj_ratings = [obj.rating for obj in objects] obj_authors = [obj.author for obj in objects] obj.titleobj.rating是如何提取对象属性的示例。我不知道确切的属性名称,因此您需要用正确的名称替换 title rating author

然后,检查用户输入的参考号,如果匹配,则打印信息

obj.author

if referencenum not in obj_references: print("No Such Object found") else: obj_id = obj_references.index(referencenum) print("Title:", obj_titles[obj_id], "Rating:", obj_ratings[obj_id], "Author:", obj_authors[obj_id]) 返回所输入参考编号的索引,使用该索引,您可以从各自的列表中提取标题,等级和作者(例如obj_id = obj_references.index(referencenum))。

完整功能如下:

obj_titles[obj_id]

答案 1 :(得分:0)

您可以使用列表推导并使用not in来代替对所有对象分别进行for循环。

def search_item():
    reference_num = input("Enter item reference:")
    if reference_num not in [obj.reference for obj in initialize()]:
        print('No such object found')