所以我需要搜索一個項目是否在列表的第一列,如果是,打印該項目。我必須使用函數來做到這一點。在列表和功能列表中搜索
CD = input("Enter name of CD database: ")
def createDatabase(CD): #opens and creates list
aList = []
file = open(CD)
for line in file:
line = line.rstrip().split(",") #strip \n and split at ,
aList.append(line) #add lines into formerly empty aList
for i in range(len(aList)):
aList[i][3] = float(aList[i][3]) #override line for price to be float
return aList
aList = createDatabase(CD)
def PrintList(aList):
for line in aList:
album = str(line[0])
artist = str(line[1])
genre = str(line[2])
price = str(line[3])
print("Album: " + album + " Artist: " + artist + " Genre: " + genre + " Price: $" + price)
return
def FindByTitle(aList):
target = input("Enter Title to Search: ")
for item in aList:
if target in aList:
print(target)
else:
print ("Title not found")
return aList
PrintList(FindByTitle(aList))
我從這個讓我的輸出是
Enter name of CD database: CD.txt
Enter Title to Search: Sempiternal
Title not found
Title not found
Title not found
Title not found
Album: Sempiternal Artist: Bring Me The Horizon Genre: Metalcore Price: $14.5
Album: Badlands Artist: Halsey Genre: Indie Pop Price: $19.95
Album: Wildlife Artist: La Dispute Genre: Post Hardcore Price: $9.6
Album: Move Along Artist: The All American Rejects Genre: Punk Rock Price: $10.2
,我不完全知道該怎麼做或解決我的搜索功能。任何幫助將非常感謝。
產生列出了一些樣本數據和輸入(與字符串替換'input'表達式)添加到你的問題,所以我們可以運行代碼。 –
'如果aList中的目標:'不正確。 '如果目標在項目中:' – ssm