Search code examples
pythonfunctionreturnreadlines

I don't know how to return and print the function result. (I am making a python address book program)



def search_namecard():
    find_IG = input("The IG of the person you are looking for : ")
    with open('memberinfo_.txt') as f:
        datafile = f.readlines()
        for line in datafile:
            if find_IG in line:
                return line
        return False

while True:
    print("INSERT(1), SEARCH(2), LIST(3), MODIFY(4), DELETE(5), EXIT(0)")
    menu = get_menu()
    if menu==1:
        new_card = get_namecard_info()
        with open("memberinfo_.txt", mode='a') as f:
                f.write(new_card.printCard())
        namecard_list.append(new_card) 

    elif menu==2:
        if search_namecard() != True :
            print(line)
        else:
            print("Not Found")

I made an address book program that receives personal information and stores it in a txt file. I am trying to add a search function, but I am using the readlines() function. When I find the Instagram ID in the address book, I want to display the information of the person, but I am curious how to return the 'line' variable out of the function.

enter image description here


Solution

  • You return line from your function as a value without a name. You need to save the value somewhere to use it like this:

    search_result = search_namecard()
    if search_result:
        print(search_result)
    else: 
        print("Not found")