以下是我遇到问题的一些代码的一部分:
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"
我有一个数据库,该程序正在从中提取数据。我需要它表现得像这样:
我遇到的两个问题是:
如何解决这些问题?
答案 0 :(得分:0)
您的sequentialSearch
功能不打印任何内容。并且您不需要found
变量,当您找到匹配项时,您可以从该函数return
获得。{/ p>
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"
答案 1 :(得分:0)
可以根据需要控制选项:
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"