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)
#π contact book wont store info properly
165 messages Β· Page 1 of 1 (latest)
@mystic ridge
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.
Closes after a period of inactivity, or when you send !close.
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
first of all, theres a typo on line 1
what the what
sef addMembers():
so, you can add a member
only 1
but when you add another member the first one is overwrittten
what exactly are you returning on your addMembers function?
new_members
it doesnt look like thats what ur returning
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
send ur full code again
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)
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
add_members variable and addMembers() function are different, kind of confusing
add_members variable stores the dict
also passing parameters to functions that arent needed
then what is members = {} for?
extra members and honestly idk why i thought this would work
and what is the purpose of updateMembers()
add more members
xd bad naming i guess
then what is the purpose of addMembers()?
its much like creating an account
1st person's info
then if the user wants to add more
you dont want help?
i do
but i think u alone cant do it
i have seen some fixes for this
but they are all temporary
whys that? xd
idk i feel shit rn
^this is the problem I think. You don't need the update function at all
ah...
i dont want to back off
if i delete the updatefunctions it would make me feel skill issues
less code is a skill ability
I want to make that work
writing too much is the skill issue
i have written it and i need to get it to work ..i will learn something maybe
Code is really big the issue is in which part
Changing your code to be better is definitely a skill, deleting code is an important part of that
The updateFunction
Ok
deleting code in my case would only make sense if i had a plan on what i would write again but i dont have any plan
have you thought about what you need to do before starting to write the program?
because thats usually a big help
like,
first step: be able to add a member
i did everything step by step and the updateMembers got me real bad
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
i dont see why it would work
ur never doing anything with members
do you know what return does?
returns the value whatever it is to function call
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
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?
returns new members to function call obv
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
what are you doing with that add_members variable afterwards?
its not being used anywhere
it is
in the function calls
but yeah otherwise no
its not being used anywhere there either
so we can remove all the parameters
ok
the add members function creates a member, it doesn't add any
it's an unfortunate function name
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
make sure to remove them at function calls aswell
done
alright, now back to the addMembers() function
what can we do instead of return new_members?
to add them to the members dictionary
and how would we do that
im pretty sure u know how dictionaries work right?
yea
in order to store something you need a key - value pair
yes
so you need an identifier for each member
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}
now the members will store members like this:
{"name" : {"number" : 123, "age" : 14}}
do you know how to do the rest?
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
if you want to challenge urself further, you can make a while True loop to keep adding members
updatemembers isnt needed π
Man
no i want to see if it works
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)
u have code that works perfectly, why add more for the same output?
I want to be able to add more people if i want to
do this instead
ok
while True:
choice = int(input("What would you like to do? (1: add, 2: delete, 3: view, 4: quit): "))
start with something like this
Why i always solve late π
ok
i will make a thing which lets us choose the function we want to execute
im gonna go sleep now
ty for helping
i never seen such a big emoji
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
seperate file??
in a .json file
im already into a course
uh ok
welp bye
!close
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.