#πŸ”’ contact book wont store info properly

165 messages Β· Page 1 of 1 (latest)

mystic ridge
#
ef addMembers():
    person_name = input("Please enter the name of the person you wish to add to your contacts.")
    person_number = int(input("Please enter the password of that person. "))
    person_age = int(input("Please enter the age of that person: "))
    new_members = {"name":person_name,"age":person_age,
    "number":person_number}
    return members
def updateMembers(update_members):
    update_choice = input("Enter the name of the person you want to update (add) to your contacts: ")
    update_choice_age = int(input("Enter the age of that person: "))
    update_choice_number = int(input("Enter the number of that person! "))
    access_dict = members[update_choice] = update_choice
    access_dict = members[update_choice_age] = update_choice_age
    access_dict = members[update_choice_number] = update_choice_number
    print(f"{update_choice} has been added to your dictionary! ")
    return access_dict
def deleteMembers(delete_members):
    delete_choice = input("Enter the name of the person you want to remove from your contacts: ")
    del members[delete_choice]
    print(f"{delete_choice} has been removed from your contacts! ")
members = {}
def viewMembers(view_members):
    view_choice = input("Do you want to view your current contact list or no? ")
    if view_choice == "yes":
        print(view_members)
    else:
        print("Ok ")


add_members = addMembers()
update_members = updateMembers(add_members)
delete_members = deleteMembers(add_members)
view_members = viewMembers(add_members)
jagged boltBOT
#

@mystic ridge

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

mystic ridge
#

The issue is that when I update the members (add new member) the old member's info is over written

#

and I need a way to store as many members as I want

#

ping me if u see this

real charm
#

first of all, theres a typo on line 1

mystic ridge
#

what the what

real charm
#

sef addMembers():

mystic ridge
#

oh wait...

#

its a mistake here only

#

k done

real charm
#

so, you can add a member

mystic ridge
real charm
#

but when you add another member the first one is overwrittten

mystic ridge
#

ye

#

i need a way to store as many members as i want

real charm
#

what exactly are you returning on your addMembers function?

mystic ridge
#

new_members

real charm
#

it doesnt look like thats what ur returning

mystic ridge
#

Uh lemme see

#

man

#

shi

#

i will fix and run

#
Please enter the name of the person you wish to add to your contacts.hammad
Please enter the password of that person. 123
Please enter the age of that person: 15
Enter the name of the person you want to update (add) to your contacts: hamza
Enter the age of that person: 16
Enter the number of that person! 26
hamza has been added to your dictionary! 
Enter the name of the person you want to remove from your contacts: hammad
Traceback (most recent call last):
  File "C:\Users\Hammad\PycharmProjects\pythonProject\ContactBook.py", line 32, in <module>
    delete_members = deleteMembers(add_members)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Hammad\PycharmProjects\pythonProject\ContactBook.py", line 19, in deleteMembers
    del members[delete_choice]
        ~~~~~~~^^^^^^^^^^^^^^^
KeyError: 'hammad'
#

that didnt work

#

also if u look carefully into the code

#
members = {}
#

this thing is supposed to store extra infos

#

but its useless

#

thats why i wrote return members

real charm
#

send ur full code again

mystic ridge
#
def addMembers():
    person_name = input("Please enter the name of the person you wish to add to your contacts.")
    person_number = int(input("Please enter the password of that person. "))
    person_age = int(input("Please enter the age of that person: "))
    new_members = {"name":person_name,"age":person_age,
    "number":person_number}
    return new_members
def updateMembers(update_members):
    update_choice = input("Enter the name of the person you want to update (add) to your contacts: ")
    update_choice_age = int(input("Enter the age of that person: "))
    update_choice_number = int(input("Enter the number of that person! "))
    access_dict = members[update_choice] = update_choice
    access_dict = members[update_choice_age] = update_choice_age
    access_dict = members[update_choice_number] = update_choice_number
    print(f"{update_choice} has been added to your dictionary! ")
    return access_dict
def deleteMembers(delete_members):
    delete_choice = input("Enter the name of the person you want to remove from your contacts: ")
    del members[delete_choice]
    print(f"{delete_choice} has been removed from your contacts! ")
members = {}
def viewMembers(view_members):
    view_choice = input("Do you want to view your current contact list or no? ")
    if view_choice == "yes":
        print(view_members)
    else:
        print("Ok ")


add_members = addMembers()
update_members = updateMembers(add_members)
delete_members = deleteMembers(add_members)
view_members = viewMembers(add_members)
real charm
#
update_members = updateMembers(add_members)
delete_members = deleteMembers(add_members)
view_members = viewMembers(add_members)

why are you passing add_members as an argument here

tropic turret
#

add_members variable and addMembers() function are different, kind of confusing

mystic ridge
#

add_members variable stores the dict

real charm
#

also passing parameters to functions that arent needed

real charm
mystic ridge
real charm
#

and what is the purpose of updateMembers()

mystic ridge
#

xd bad naming i guess

real charm
mystic ridge
#

1st person's info

#

then if the user wants to add more

real charm
#

but you have 2 functions doing practically the same thing

#

is there 1 main user?

mystic ridge
#

no..

#

i will ask someone else to jump in

real charm
#

you dont want help?

mystic ridge
#

but i think u alone cant do it

#

i have seen some fixes for this

#

but they are all temporary

real charm
mystic ridge
real charm
#

why?

#

everyone starts somewhere

tropic turret
mystic ridge
#

i dont want to back off

#

if i delete the updatefunctions it would make me feel skill issues

golden trout
#

less code is a skill ability

mystic ridge
golden trout
#

writing too much is the skill issue

mystic ridge
timber torrent
#

Code is really big the issue is in which part

tropic turret
#

Changing your code to be better is definitely a skill, deleting code is an important part of that

mystic ridge
timber torrent
mystic ridge
real charm
#

have you thought about what you need to do before starting to write the program?

#

because thats usually a big help

mystic ridge
#

um what

#

not really

#

i need to write the program

#

what i want it to do yk

real charm
#

like,
first step: be able to add a member

mystic ridge
real charm
#

try removing the updateMembers function

#

it will make it better

mystic ridge
#

ok

#
Please enter the name of the person you wish to add to your contacts.hammad
Please enter the password of that person. 15
Please enter the age of that person: 14
Enter the name of the person you want to remove from your contacts: hammad
def addMembers():
    person_name = input("Please enter the name of the person you wish to add to your contacts.")
    person_number = int(input("Please enter the password of that person. "))
    person_age = int(input("Please enter the age of that person: "))
    new_members = {"name":person_name,"age":person_age,
    "number":person_number}
    return new_members

def deleteMembers(delete_members):
    delete_choice = input("Enter the name of the person you want to remove from your contacts: ")
    del members[delete_choice]
    print(f"{delete_choice} has been removed from your contacts! ")
members = {}
def viewMembers(view_members):
    view_choice = input("Do you want to view your current contact list or no? ")
    if view_choice == "yes":
        print(view_members)
    else:
        print("Ok ")


add_members = addMembers()

delete_members = deleteMembers(add_members)
view_members = viewMembers(add_members)
#

the delete wont work now

real charm
#

i dont see why it would work

#

ur never doing anything with members

#

do you know what return does?

mystic ridge
#
def deleteMembers(delete_members):
    delete_choice = input("Enter the name of the person you want to remove from your contacts: ")
    del members[delete_choice]
    print(f"{delete_choice} has been removed from your contacts! ")
members = {}

i cant delete if i dont have the members thing

real charm
#

im not saying to remove the members variable

#

but ur never adding anything to it

#
def addMembers():
    person_name = input("Please enter the name of the person you wish to add to your contacts.")
    person_number = int(input("Please enter the password of that person. "))
    person_age = int(input("Please enter the age of that person: "))
    new_members = {"name":person_name,"age":person_age,
    "number":person_number}
    return new_members
#

lets break down this function

#

return new_members, what does this do?

timber torrent
#

Yes return Is more powerful than print in a function

#

Someone were told this

mystic ridge
golden trout
#

there's a fundamental issue here:
members is a dict
each member is a dict

either you need to make members a list instead, or you need to make members a mapping of name to member

real charm
#

its not being used anywhere

mystic ridge
#

in the function calls

#

but yeah otherwise no

real charm
#

so we can remove all the parameters

mystic ridge
#

ok

golden trout
#

it's an unfortunate function name

mystic ridge
#
def addMembers():
    person_name = input("Please enter the name of the person you wish to add to your contacts.")
    person_number = int(input("Please enter the password of that person. "))
    person_age = int(input("Please enter the age of that person: "))
    new_members = {"name":person_name,"age":person_age,
    "number":person_number}
    return new_members

def deleteMembers():
    delete_choice = input("Enter the name of the person you want to remove from your contacts: ")
    del members[delete_choice]
    print(f"{delete_choice} has been removed from your contacts! ")
members = {}
def viewMembers():
    view_choice = input("Do you want to view your current contact list or no? ")
    if view_choice == "yes":
        print(view_members)
    else:
        print("Ok ")


add_members = addMembers()

delete_members = deleteMembers(add_members)
view_members = viewMembers(add_members)

i removed em

real charm
#

make sure to remove them at function calls aswell

mystic ridge
#

done

real charm
#

alright, now back to the addMembers() function

#

what can we do instead of return new_members?

#

to add them to the members dictionary

mystic ridge
#

and how would we do that

real charm
#

im pretty sure u know how dictionaries work right?

mystic ridge
#

yea

real charm
#

in order to store something you need a key - value pair

mystic ridge
#

yes

real charm
#

so you need an identifier for each member

mystic ridge
#

wait

#

are we storing the whole new_members into the members dict

real charm
#

not exactly

#

first we need an identifier for the member

#

instead of

new_members = {"name":person_name,"age":person_age,
    "number":person_number}
#

how about we do

members[person_name] = {"number": person_number, "age": person_age}
mystic ridge
#

dayum

#

i didnt think bout that

real charm
#

now the members will store members like this:
{"name" : {"number" : 123, "age" : 14}}

mystic ridge
#

oh yeah

#

now we talking

real charm
#

do you know how to do the rest?

mystic ridge
#

i will make a view function to test

#
Please enter the name of the person you wish to add to your contacts.hammad
Please enter the password of that person. 123
Please enter the age of that person: 15
Enter the name of the person you want to remove from your contacts: hammad
hammad has been removed from your contacts! 
Do you want to view your current contact list or no? yes
{}
#

ok it worked

#

bro danggg

#

i was stuck for 6 hours or so

#

let me try updateMembers

real charm
#

if you want to challenge urself further, you can make a while True loop to keep adding members

real charm
timber torrent
#

Man

mystic ridge
#

no i want to see if it works

timber torrent
#

Here is a good code but have a little issue on calling those functions

#


def addMembers():
    person_name = input("Please enter the name of the person you wish to add to your contacts.")
    person_number = int(input("Please enter the password of that person. "))
    person_age = int(input("Please enter the age of that person: "))
    new_members = {"name":person_name,"age":person_age,
    "number":person_number}
    return new_members

def deleteMembers():
    delete_choice = input("Enter the name of the person you want to remove from your contacts: ")
    del new_members[delete_choice]
    print(f"{delete_choice} has been removed from your contacts! ")
members = {}
def viewMembers():
    view_choice = input("Do you want to view your current contact list or no? ")
    if view_choice == "yes":
        print(view_members)
    else:
        print("Ok ")


add_members = addMembers()

delete_members = deleteMembers(add_members)
view_members = viewMembers(add_members)


real charm
mystic ridge
real charm
#
while True:
    choice = int(input("What would you like to do? (1: add, 2: delete, 3: view, 4: quit): "))

start with something like this

timber torrent
mystic ridge
#

i will make a thing which lets us choose the function we want to execute

#

im gonna go sleep now

#

ty for helping

mystic ridge
real charm
#

another thing you could do after that is try to store all the information in a seperate file, so you dont need to add members every time you run the program

#
mystic ridge
#

seperate file??

real charm
#

in a .json file

mystic ridge
#

uh ok

#

welp bye

#

!close

jagged boltBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.