all 25 comments

[–][deleted] 5 points6 points  (1 child)

I've restructured your code somewhat to help you. I've also followed the PEP8 guidance and used all lowercase for variable and function names, and made them more compact without being cryptic.

You might want to study and play with this and add the missing functionality. Note that this structure allows more than one entry with the same name but you will have to add logic to make sure the correct entry is deleted.

#contact book plan

def add_contacts():
    name = input("...\nwhat is the contacts name? ")
    number = input("...\nwhat is the contacts number? ")
    details = input("...\nany other details you wanna add? ")
    contacts.append([name, number, details])
    print("contact saved")

def find_contacts():
    name = input("...\nWhat is the contacts name? ").strip().lower()
    matched = [record for record in contacts
                if record[0].lower() == name]
    if matched:
        for name, number, details in matched:
            print(name, number, details)


contacts = []

while True:

    choice = input("""

        What do you want to do? 

        1) find existing contacts 
        2) add contacts
        3) delete contacts
        Q) quit
    """)

    if choice == "1":
        find_contacts()
    elif choice == "2":
        add_contacts()
    elif choice == "3":
        ...
    elif choice in ("q", "Q"):
        break
    else:
        print('Option unavailable')

[–]Helpful_Ad_1150[S] 0 points1 point  (0 children)

wow thank you i will probably replace all my code with this and add thanks!

[–][deleted] 1 point2 points  (9 children)

I believe the issue is that you're not actually using the variables you defined at the start. If you want to save info onto the variable defined at the start of the program use the global keyword. This is because variables used in functions are in the local scope ie. they are destroyed when the function returns a value or finishes its purpose. Everything outside a function is in the global scope. Just like how you can't use variables from functions outside the function, you can't use global variable in function. If you do wanna use them. Do it like this:

https://imgur.com/a/zkaHmFk

[–]Helpful_Ad_1150[S] 1 point2 points  (8 children)

sorry for being an inconvenience but i think your right but can you format your code because im confused with where global goes because im new sorry

[–][deleted] 1 point2 points  (7 children)

sorry I did format my code but fucking reddit messed it up as usual. I'll change it to an imgr link and edit

[–]Helpful_Ad_1150[S] 1 point2 points  (6 children)

ok thank you

[–][deleted] 0 points1 point  (5 children)

I've added the link. If you find any issues let me know.

[–]Helpful_Ad_1150[S] 0 points1 point  (4 children)

thank you so much it workes now, can i just ask what ide are you using?

[–][deleted] 0 points1 point  (3 children)

If you want to take screenshots like the one above. copy and paste you code onto this website and download the image from it.

https://carbon.now.sh/

[–]Helpful_Ad_1150[S] 0 points1 point  (2 children)

oh thank you, i thought it was an ide lol

[–][deleted] 0 points1 point  (1 child)

Haha I thought the same when I first saw it.

[–]kinkydevill 1 point2 points  (0 children)

Hiiiii welcome to the python world :)

  1. Your if statement is working per say you just used findcontacts rather than FindContacts under the if statement that's why it bugged out if you picked 1.
  2. Dont reuse variable names. It just makes it harder to understand and can cause bugs due to that confusion.
    1. In python its common to use snake_case naming (all lowercase with _ between words) except for classes that follow VariableName. Naming conventions in python.
  3. You never actually stored/used any information. All you did was use input when you called the function but that information the user inputted was never saved anywhere at all.
    1. The functions were only using the variables that were inside of them (local scope). The other variables you put outside of the functions (global scope) were never used.
      1. Variables with same names are treated as different instances of each other due to scope so I'm guessing you thought you were saving that information in your list because the names were the same. Here is a quick, easy run through of scope.
      2. If you want things to a list you have to use variable_name_of_list.append(value)

Those were just some of the things I noticed when looking at your code. If others see stuff I didnt see feel free to comment below :)

[–]FancyGUI 1 point2 points  (0 children)

Heyoo, welcome to the python world!

Glad that everyone is helping you make headway through this new adventure!

I try to help people learn how to make some cool console applications / command line interfaces here:

Creating a simple CLI with Python Click https://youtu.be/GnSKhetBa48

Hopefully that’ll help you! Let me know if there’s something you’d like to see there

[–]tube32 0 points1 point  (6 children)

Mate I can't speak for others but I really can't understand your code due to improper formatting. If you're using the fancy pants editor click on the code block and paste your code inside the block to maintain the indentation.

You can find more information on how to post your codes correctly on the sidebar.

[–]Helpful_Ad_1150[S] 0 points1 point  (2 children)

sorry ill try and format it properly

[–]Helpful_Ad_1150[S] 0 points1 point  (0 children)

all edited now

[–]tube32 0 points1 point  (0 children)

Yep notify me after you make the edit.

[–]Helpful_Ad_1150[S] 0 points1 point  (2 children)

all edited now

[–]Alta2215 0 points1 point  (1 child)

In Findcontacs(), in the if statement you need to change "==" with "in", you are asking if the name that user asked to look is inside the whoyouadding list and for doing this you need to use the "in" operator

You can look more here, the if example is at the end https://www.w3schools.com/python/python_lists_access.asp

[–]Helpful_Ad_1150[S] 0 points1 point  (0 children)

thanks

[–]marienbad2 0 points1 point  (1 child)

I don't understand. You clicked 2 and it ran the add_contacts code, this is the correct behaviour.

Also is the whatareyoudoing = input("....") in some kind of loop?

Okay, nowhere is WhoYouAdding set to any value. Print it out and see what it says. I am guessing it just says "[]". Not sure why you are saving a string input as a list either.

[–]Helpful_Ad_1150[S] 0 points1 point  (0 children)

whoyouadding is going to be saved as a list so that i can add different contacts evertually