打印列表在python中更令人赏心悦目

时间:2019-03-06 10:08:10

标签: python python-3.x

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')作为列表

1 个答案:

答案 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