rockethon
rockethon

Reputation: 37

List Address Book

Why is my list empty when I ask python on the shell to print it a 2nd time?

def name():
    name = str(input("Name:"))
    return name

def phone():
    phone = str(input("Phone Number: "))
    return phone

def email():
    email = str(input("Email: "))
    return email

Then I created a join function in order for me to add all the data to a list:

def join(lst, name, phone, email): 
    name = name()

if name in lst:
    print("Name already in")
    phone = phone()

if phone in lst:
    print("Phone already exists")
    email = email()

if email in lst:
    print("Email already exists!")
    lst += [name]
    lst += [phone]
    lst += [email]
    print("Added!")
return lst

Function to list every entry on the Address book:

def print_list(lst):
    print(lst)

And the menu looks like this:

def menu():
    print("1-New Contact")
    print("2-Print list")
    option = int(input("Option:"))
return option

def repeat(lst):
    option = menu()
    while option < 2 and option >= 1:
        if option == 1:
            lst = join(lst, name, phone, email)
        elif opcao == 2:
            lst = print_list(lst)
        else:
            print("Invalid option")
        opcao = menu()

lst = []
call = repeat(lst)
print(call)

Upvotes: 1

Views: 782

Answers (1)

Szymon
Szymon

Reputation: 43023

I assume by printing you mean this part of the code: lst=print_list(lst) and your list is in lst variable.

What you do here is assign the return value of print_list function to your lst variable after the first call. This will set it to None as print_list doesn't return any value explicitly. When you try to print the second time, lst is None and nothing is printed.

You should not assign to lst and simply call print_list(lst) without assigning the return value:

elif opcao==2: print_list(lst)

Upvotes: 2

Related Questions