【问题标题】:Sequential searching of a database数据库的顺序搜索
【发布时间】:2014-12-27 05:20:16
【问题描述】:

这是我遇到问题的一些代码的一部分:

def func():
    for i in range(len(titleList)):
        print titleList[i] + ' -- ' + artistList[i] + ' -- ' + albumList[i] + ' --',
        print yearList[i] + ' -- ' + commentList[i]

def sequentialSearch(albumList, item):
    pos = 0
    found = False
    while pos < len(albumList) and not found:
        if albumList[pos] == item:
            found = True
        else:
            pos = pos + 1
    return found

num = input("What would you like to do?\n1. Print all the contents of the database\n2. Find all the songs on a particular album\n3. Quit\nPlease Enter 1, 2, or 3: ")
if num == 3:
    print "Goodbye"
else:
    while num != 3:
        if num == 1:
            func()
        if num == 2:
            item = raw_input("Please enter the album: ")
            seqentialSearch(albumList, item)
        else:
            print "Invald Input"

我有一个数据库,该程序从中提取数据。我需要它表现得像这样:

  • 如果输入 1,程序将返回数据库的全部内容
  • 如果输入 2,我希望程序使用顺序搜索来打印有关该专辑中歌曲的所有信息
  • 如果输入 3,我只想打印“再见”

我遇到的两个问题是:

  1. 我的顺序搜索代码未返回正确的信息
  2. 我希望程序循环直到输入 3。

我该如何解决这些问题?

【问题讨论】:

    标签: python loops search


    【解决方案1】:

    您的sequentialSearch 函数没有打印任何内容。而且你不需要found 变量,你可以在找到匹配项时从函数中找到return

    def sequentialSearch(albumList, item):
        for i in range(len(albumlist))
            if albumList[i] == item:
                print titleList[i] + ' -- ' + artistList[i] + ' -- ' + albumList[i] + ' --',
                print yearList[i] + ' -- ' + commentList[i]
                return true
        return false
    

    在您的主代码中,您的 while 循环将永远不会结束,因为您在循环外请求 num。使用:

    while true:
        num = input("What would you like to do?\n1. Print all the contents of the database\n2. Find all the songs on a particular album\n3. Quit\nPlease Enter 1, 2, or 3: ")
        if num == 3:
            print "Goodbye"
            break
        elif num == 1:
            func()
        elif num == 2:
            item = raw_input("Please enter the album: ")
            sequentialSearch(albumList, item)
        else:
            print "Invald Input"
    

    【讨论】:

    • 谢谢大家@barmar,您的顶级函数返回以下错误:Traceback(最近一次调用最后一次):文件“F:/Downloads/Python/Mods/MusicOrganizer.py”,第 51 行,在 seqentialSearch(albumList, item) NameError: name 'seqentialSearch' is not defined
    • 你拼错了sequential,你错过了u,我复制了你写的内容。
    【解决方案2】:

    这样可以根据需要控制选项:

    num = 0
    while num !=3:
        num = input("What would you like to do?\n1. Print all the contents of the database\n2. Find all the songs on a particular album\n3. Quit\nPlease Enter 1, 2, or 3: ")
        if num == 1:
            func()
        elif num == 2:
            item = raw_input("Please enter the album: ")
            seqentialSearch(albumList, item)
        elif num != 3:
            print "Invald Input"
    print "Goodbye"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-01
      • 1970-01-01
      相关资源
      最近更新 更多