#๐ Need to clear some concepts
254 messages ยท Page 1 of 1 (latest)
@honest flume
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.
Please ping me if someone seens this
what kind of clarification are you looking for?
from a quick glance,
- both deposite and withdraw are done on the original account. So you're creating copies of them rather than manipulating the same data
- This would be a great case for Class. Have you learned about Class yet?
@vernal canyon
def createAccount():
person_user = input("Make a username: ")
person_pass = input("Make a password: ")
print("Your account has been created successfully! ")
return {"username":person_user,"password":person_pass
,"balance":0}
def depositMoney(account_call):
deposit_choice = int(input("Enter the amount of money you want to deposit: "))
account_call["balance"] += deposit_choice
print(f"${deposit_choice} have been deposited to your account")
return account_call
def withdrawMoney(account_withdraw):
withdraw_choice = int(input("Enter the amount of money you want to withdraw: "))
account_withdraw["balance"] -= withdraw_choice
print(f"$ {withdraw_choice} have been withdrawn from your account! ")
return account_withdraw
def checkAccount(account_check):
check_choice = input("Do you want to check the balance in your account?y/n ")
if check_choice == "y":
print(f"Your account balance is: ${account_check['balance']}")
return account_check
account_create = createAccount()
account_deposit = depositMoney(account_create)
account_withdraw = withdrawMoney(account_create)
account_check = checkAccount(account_create)
So in this banking system
Can you explain me the calling of the functions?
what u mean
Uh like
how the parameters were able to access the dictionary containing the user info
the function returns a dictionary. So that get stores in account_create
the rest are the same thing - variables storing dictionary returned from functions
you need to take proper basic lessons
no i know that stuff when i look at it all at once yeah my brain stops braining
call the first function, returns a dictionary
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 name of that person. "))
print("That person has been added to your contact book! ")
return {"personName":person_name,"personNum":person_number}
def updateMembers(update_members):
new_update = input("Enter the name of the perosn you want to add to your contact book")
update_members["personName"]
members = addMembers()
members_update = updateMembers(members)
and in this case...if i had to update the members listh ow would i upgrade the nameo f ther person? with a colon? ":"
pass that dictionary as a parameter to depositmoney and call it
it returns the same dictionary
call withdrawmoney and pass this dictionary to it
it returns the same dictionary
so the dict just goes on and on
this?
im learning how to store things lately
when you do this, you might want to update the same variables rather than having new ones
in this u dont return nothing
in the second function
update_members["personName"]
im confused about this part on how to update
account = depositMoney(account)
account = withdrawMoney(account)
account = checkAccount(account)```
its not finished yet
when u want to be in person name store it there
that's usually how you do it - updating value that is
uh ok lemme try
update_members["personName"] = "somename"
new_update = input("Enter the name of the perosn you want to add to your contact book")
update_members["personName":new_update]
```?
oops
yeah
treat update_members["personName"] as a variabble
i was doing numbers so i thought with letters i could do some different
ok
u can store whatever u want in it
and do whatever operations that variable supports
new_update = input("Enter the name of the perosn you want to add to your contact book")
update_members["personName"] = new_update
?
ye
k
also there's a thing called mutable and immutable types
a dictionary is a mutable type
can be changed?
ye exactly
str, int, tuple are immutable
means can't be changed
list, dictionary are mutable
also
i need to create variable update_members once again to add in the pass of the user? along with a new input asking for password?
def createAccount():
person_user = input("Make a username: ")
person_pass = input("Make a password: ")
print("Your account has been created successfully! ")
return {"username":person_user,"password":person_pass
,"balance":0}
def depositMoney(account_call):
deposit_choice = int(input("Enter the amount of money you want to deposit: "))
account_call["balance"] += deposit_choice
print(f"${deposit_choice} have been deposited to your account")
def withdrawMoney(account_withdraw):
withdraw_choice = int(input("Enter the amount of money you want to withdraw: "))
account_withdraw["balance"] -= withdraw_choice
print(f"$ {withdraw_choice} have been withdrawn from your account! ")
def checkAccount(account_check):
check_choice = input("Do you want to check the balance in your account?y/n ")
if check_choice == "y":
print(f"Your account balance is: ${account_check['balance']}")
my_account = createAccount()
depositMoney(my_account)
withdrawMoney(my_account)
checkAccount(my_account)
my_account gets updated over and over again
oh cuz the dict is the only thing returning all over the functions?
no because u change the same dictionary in all the functions
ohhhh
so u dont need to return it and store in a new variable if u dont want to
u can just use the original dictionary
ok
new_update = input("Enter the name of the person you want to add to your contact book")
new_update_pass = int(input("Enter the pass of that person"))
update_members["personName"] = new_update
update_members["personNum"] = new_update_pass
does that work
sure
lets see
Your contact book currently has 0 members!
Please enter the name of the person you wish to add to your contacts.Mtn
Please enter the name of that person. 123
That person has been added to your contact book!
Enter the name of the person you want to add to your contact bookjustme
Enter the pass of that person12345
justme person has been added to your contact book
sike
the "Mtn" is my answer idk why it wasnt blue lol
I will make it better now
I still need to do delete and view contacts btw
Ngl i didnt know we could do shit with dicts using parameters up until a few hours ago
uh
hm?
the Mtn on line 3 and the 2nd last line
oh, because blue in that case is a number
oh right
yea
k welp ima do the 2 things left
what function can i use to delete something from a dict?
u can do it with lists as well by the way
i think dicts is simpler
dicts is for one thing
lists for another
lists are mutable too
u can mutate them inside functions
well that maybe no
whats the name of the function?
ok i will look into dat
del some_dict["my_key"]
keys can also be any immutable values, more or less
so the key = value so both get deleted right
once the key gets deleted
the value related to it is deleted too
delete_members = input("Enter the name of the person you want to delete from your contact book: ")
deleteMembers[delete_members]
this good?
delete_members = input("Enter the name of the person you want to delete from your contact book: ")
del deleteMembers[delete_members]
u can delete one key at a time
thought so
u called the variable delete_members
i cant get the actual dict here tho
print("Your contact book currently has 0 members! ")
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 name of that person. "))
print(f"{person_name} has been added to your contact book! ")
return {"personName":person_name,"personNum":person_number}
def updateMembers(update_members):
new_update = input("Enter the name of the person you want to add to your contact book ")
new_update_pass = int(input("Enter the password of that person "))
update_members["personName"] = new_update
update_members["personNum"] = new_update_pass
print(f"{new_update} has been added to your contact book")
def deleteMembers(delete_members):
delete_members = input("Enter the name of the person you want to delete from your contact book: ")
del [delete_members]
members = addMembers()
members_update = updateMembers(members)
it aint even assinged to a varible
del some_dict[account_to_delete]
i dont understand? there exists no variable like account to delete
thats the input
thats what u get from the input
a name of an account to delete
then u remove it from the dictioanry
try to understand what u type
u keep on writing code without thinking
how do i access the dict?
i try my best
pass it as a parameter
def a(my_dict):
my_input = input
del my_dict[my_input]
u dont necessarily need a variable to return
thats valid too
but what would be the name of the dict? if theres no variable
ohhhh
hold on then
def deleteMembers(delete_accounts):
delete_members = input("Enter in the account you want to delete: ")
del delete_accounts[delete_members]
crap.
i didnt return
im scared lol
some_dict = {}
delete_members(some_dict)
Your contact book currently has 0 members!
Please enter the name of the person you wish to add to your contacts.Hammad
Please enter the name of that person. 123
Hammad has been added to your contact book!
Enter the name of the person you want to add to your contact book You
Enter the password of that person 135
You has been added to your contact book
i forgot to call the function xD
hold on im getting rain of errors
print("Your contact book currently has 0 members! ")
def addMembers():
person_name = "some_name"# input("Please enter the name of the person you wish to add to your contacts.")
person_number = int(input("Please enter the name of that person. "))
print(f"{person_name} has been added to your contact book! ")
return {"personName":person_name,"personNum":person_number}
def updateMembers(update_members):
new_update = input("Enter the name of the person you want to add to your contact book ")
new_update_pass = int(input("Enter the password of that person "))
update_members["personName"] = new_update
update_members["personNum"] = new_update_pass
print(f"{new_update} has been added to your contact book")
def deleteMembers(delete_members):
delete_members = input("Enter the name of the person you want to delete from your contact book: ")
del [delete_members]
members = addMembers()
members_update = updateMembers(members)
watch what i did in the person name
keep doing it for all the inputs
except the one u want to test
print("Your contact book currently has 0 members! ")
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 name of that person. "))
print(f"{person_name} has been added to your contact book! ")
return {"personName":person_name,"personNum":person_number}
def updateMembers(update_members):
new_update = input("Enter the name of the person you want to add to your contact book ")
new_update_pass = int(input("Enter the password of that person "))
update_members["personName"] = new_update
update_members["personNum"] = new_update_pass
print(f"{new_update} has been added to your contact book")
def deleteMembers(delete_accounts):
delete_members = input("Enter in the account you want to delete: ")
del delete_accounts[delete_members]
members = addMembers()
members_update = updateMembers(members)
members_delete = deleteMembers(members)
the error:
Enter in the account you want to delete: hammad
Traceback (most recent call last):
File "C:\Users\Hammad\PycharmProjects\pythonProject\ContactBook.py", line 20, in <module>
members_delete = deleteMembers(members)
^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Hammad\PycharmProjects\pythonProject\ContactBook.py", line 16, in deleteMembers
del delete_accounts[delete_members]
~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
KeyError: 'hammad'
no account named hammad
but i did make one
u can change the value in them
return {"personName":person_name,"personNum":person_number}
i dont need a seperate space for the 2nd dude i add in updates right
but it said no key so key is the issue?
the value instead of key?
acounts[person_name] = [some_arg1, some_arg2,, ....]
put the person name as the key
update_members[personName] = new_update
update_members[personNum] = new_update_pass
like dat?
members = {}
member_name = add_member(members) # return the created member name
update_member(members[member_name])
members = {}
member_name = add_member(members) # return the created member name
update_member(members[member_name])
uh
u can make each member to be a dict as well
is this for the first function
acounts[person_name] = {"number": 5, "last_name": "whatever"}
ye
either return the member name or the created member in general
member variable
ye
i hate when i have to mess up the foundation of my code ngl
def create_member(members):
member_name = input("Please enter the name of the person you wish to add to your contacts.")
member_number = int(input("Please enter the password (number) of that person. "))
members[member_name] = {"number": member_number}
return members[member_name]
members = {}
member = create_member(members)
?
please enter pass* of that person
uh its your iunput not mine
...
how are u using the dict when its defined after
ni the members[member_name]
what u mean
ye
members[member_name] = {"number": member_number}
this line
how are u using members when the actual dict is defined below
that means i'm creating a dict with "number" in it and place it in the member_name key of members
i passed it as parameter
its not the same members
i now understand your question
so confusing
oh
its the members parameter
so u are using members parameter here to um create a dict with keys and valeus?
i explained what i did
i created a dictionary with number in it
and placed it in the key member_name of members
that dictionary resembles the new member that got created
i will put it again so i dont have to scroll
def create_member(members):
member_name = input("Please enter the name of the person you wish to add to your contacts.")
member_number = int(input("Please enter the password (number) of that person. "))
members[member_name] = {"number": member_number}
return members[member_name]
members = {}
member = create_member(members)
and then i return the new created member
Alright
also u can do this
after the inputs
i'd probably actually do it like that in real code:
def create_member(members):
member_name = input("Please enter the name of the person you wish to add to your contacts.")
member_number = int(input("Please enter the password (number) of that person. "))
member = {"number": member_number}
members[member_name] = member
return member
members = {}
member = create_member(members)
or something like this
i was just trying to understand the other one...
i kinda understand uh
u can put the member name inside it as well if u want
member = {"number": member_number, "name": member_name}
having the name as the key though is easy for access
thats why having it as a key is good
def create_member(members):
member_name = input("Please enter the name of the person you wish to add to your contacts.")
member_number = int(input("Please enter the password (number) of that person. "))
member = {"name": member_name, "number": member_number}
members[member_name] = member
return member
members = {}
member = create_member(members)
in the 2nd one members[member_name] = member
you are assigning whatever the name is to the dict?
i'm assigning the member to the key member_name of members
yeah right
that way if i want to access that member
ok i get it
all i need is his name
ok
cya
cya
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.