def search(fru):
myfile = open("sample.txt")
fruit = myfile.read()
fruit = fruit.splitlines() #Converts the contents of a file to a list
if fru in fruit:
val = "is there in the list",fru
return val
else:
return "Not there in the list "
send = input("Enter fruit to search ")
result = search(send)
print(result)
if type(result) == list:
for item in result:
print(item)
上面是我的代码,我打算获取类似香蕉的输出,但输出正在获取('banana','is in list')作为列表
答案 0 :(得分:-1)
也许您要替换此代码示例:
if fru in fruit:
val = "is there in the list",fru
return val
具有:
if fru in fruit:
val = "{} is there in the list".format(fru)
return val