-1

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

이재하
  • 1
  • 5

2 Answers2

0

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")
Eumel
  • 1,298
  • 1
  • 9
  • 19
0

You need to store the return of your function in a local variable outside the if clause so you can print it later. Something like this

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:
        line = search_namecard()
        if line:
            print(line)
        else:
            print("Not Found")

Here you can use line directly in the if since any string that it returns (other than an empty string) will assume a truth value.

Hemerson Tacon
  • 2,419
  • 1
  • 16
  • 28