#๐Ÿ”’ Need to clear some concepts

254 messages ยท Page 1 of 1 (latest)

mild birchBOT
#

@honest flume

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.

honest flume
#

Please ping me if someone seens this

vague jungle
#

what kind of clarification are you looking for?

#

from a quick glance,

  1. both deposite and withdraw are done on the original account. So you're creating copies of them rather than manipulating the same data
  2. This would be a great case for Class. Have you learned about Class yet?
honest flume
#

@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?

vernal canyon
#

what u mean

honest flume
#

Uh like

vague jungle
#

see the first one

#

account_create = createAccount()

honest flume
#

how the parameters were able to access the dictionary containing the user info

vague jungle
#

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

honest flume
vernal canyon
#

call the first function, returns a dictionary

honest flume
#
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? ":"

vernal canyon
#

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

honest flume
#

so the dict just goes on and on

vernal canyon
#

call last function and same

#

yes

honest flume
#

im learning how to store things lately

vague jungle
#

when you do this, you might want to update the same variables rather than having new ones

vernal canyon
#

in the second function

honest flume
vague jungle
#
honest flume
vernal canyon
#

when u want to be in person name store it there

vague jungle
#

that's usually how you do it - updating value that is

honest flume
#

uh ok lemme try

vernal canyon
#

update_members["personName"] = "somename"

honest flume
#
new_update = input("Enter the name of the perosn you want to add to your contact book")
    update_members["personName":new_update]
```?
#

oops

vernal canyon
#

no

#

look in the first code of the bank u sent

honest flume
#

yeah

vernal canyon
#

treat update_members["personName"] as a variabble

honest flume
#

i was doing numbers so i thought with letters i could do some different

vernal canyon
#

u can store whatever u want in it

#

and do whatever operations that variable supports

honest flume
#
new_update = input("Enter the name of the perosn you want to add to your contact book")
    update_members["personName"] = new_update

?

vernal canyon
#

ye

honest flume
#

k

vernal canyon
#

also there's a thing called mutable and immutable types

#

a dictionary is a mutable type

honest flume
#

can be changed?

vernal canyon
#

ye exactly

#

str, int, tuple are immutable

#

means can't be changed

#

list, dictionary are mutable

honest flume
#

also

vernal canyon
#

so u dont need to return stuff

#

for example

honest flume
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")

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

honest flume
#

oh cuz the dict is the only thing returning all over the functions?

vernal canyon
#

no because u change the same dictionary in all the functions

honest flume
#

ohhhh

vernal canyon
#

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

honest flume
#

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

vernal canyon
#

sure

honest flume
#

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

vernal canyon
#

uh

honest flume
vernal canyon
#

what went blue

#

wasn't blue*

honest flume
vernal canyon
#

oh, because blue in that case is a number

honest flume
#

oh right

vernal canyon
#

it recognizes python keywords

#

u see pass is red

honest flume
#

yea

#

k welp ima do the 2 things left

#

what function can i use to delete something from a dict?

vernal canyon
#

u can do it with lists as well by the way

honest flume
#

i think dicts is simpler

vernal canyon
#

dicts is for one thing

#

lists for another

#

lists are mutable too

#

u can mutate them inside functions

honest flume
#

yeah..

#

aint no way im changing it to lists ngl

vernal canyon
#

well that maybe no

honest flume
vernal canyon
#

but if u'll need lists for other stuff

#

u'll be able to do it the same way

honest flume
#

ok i will look into dat

vernal canyon
#

keys can also be any immutable values, more or less

honest flume
vernal canyon
#

for example can be an int too

#

some_dict[3] valid

vernal canyon
#

the value related to it is deleted too

honest flume
#
delete_members = input("Enter the name of the person you want to delete from your contact book: ")
    deleteMembers[delete_members]

this good?

vernal canyon
#

u need del

#

del some_dict[some_key]

honest flume
#
delete_members = input("Enter the name of the person you want to delete from your contact book: ")
    del deleteMembers[delete_members]
vernal canyon
#

u can delete one key at a time

honest flume
vernal canyon
#

u called the variable delete_members

honest flume
vernal canyon
#

it needs to be

#

account_to_delete = input

honest flume
#
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

vernal canyon
#

del some_dict[account_to_delete]

honest flume
#

i dont understand? there exists no variable like account to delete

vernal canyon
#

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

honest flume
honest flume
vernal canyon
#

pass it as a parameter

honest flume
#

ok

#
return {"personName":person_name,"personNum":person_number}

i need a variable?

vernal canyon
#
def a(my_dict):
  my_input = input
  del my_dict[my_input]
vernal canyon
#

thats valid too

honest flume
#

but what would be the name of the dict? if theres no variable

vernal canyon
#

whatever u return it to

#

x = some_func()

#

x gets the return

honest flume
#

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

vernal canyon
#

u dont need to return

#

dictionary is mutable

#

but if u want u can

#

๐Ÿคทโ€โ™‚๏ธ

honest flume
#

im scared lol

vernal canyon
#
some_dict = {}

delete_members(some_dict)
honest flume
#
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

vernal canyon
#

if u want to test a specific thing

#

inputs waste time

#

so do it like this:

honest flume
#

hold on im getting rain of errors

vernal canyon
#
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

honest flume
#
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'
vernal canyon
#

no account named hammad

honest flume
#

but i did make one

vernal canyon
#

thats what it says

#

no key hammad

honest flume
#

bruh shi

#

man the keys are strings

#

can i overwrite them or sum

vernal canyon
#

u can change the value in them

honest flume
#
return {"personName":person_name,"personNum":person_number}

i dont need a seperate space for the 2nd dude i add in updates right

honest flume
vernal canyon
#

ye

#

person name is the key

honest flume
#

im done for

#

we cant do anything about keys

vernal canyon
#

u need a better design

#

accounts[person_name] = whatever

#

can be a list

honest flume
vernal canyon
#

acounts[person_name] = [some_arg1, some_arg2,, ....]

vernal canyon
honest flume
#
update_members[personName] = new_update
    update_members[personNum] = new_update_pass
#

like dat?

vernal canyon
#
members = {}

member_name = add_member(members)  # return the created member name
update_member(members[member_name])
honest flume
#

uh

vernal canyon
#

u can make each member to be a dict as well

honest flume
vernal canyon
#

acounts[person_name] = {"number": 5, "last_name": "whatever"}

vernal canyon
#

either return the member name or the created member in general

honest flume
#

member variable

vernal canyon
#

ye

honest flume
#

i hate when i have to mess up the foundation of my code ngl

vernal canyon
#
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)
honest flume
#

ok let me just understand wats going on

#

ur 2nd input is wrong btw

vernal canyon
#

?

honest flume
#

please enter pass* of that person

vernal canyon
#

uh its your iunput not mine

honest flume
#

...

honest flume
#

ni the members[member_name]

vernal canyon
#

what u mean

honest flume
#

the parameter members

#

its the dict right

vernal canyon
#

ye

honest flume
#
  members[member_name] = {"number": member_number}
#

this line

#

how are u using members when the actual dict is defined below

vernal canyon
#

that means i'm creating a dict with "number" in it and place it in the member_name key of members

vernal canyon
#

its not the same members

#

i now understand your question

honest flume
vernal canyon
#

its the members parameter

honest flume
vernal canyon
#

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

honest flume
#
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)
vernal canyon
#

and then i return the new created member

honest flume
#

Alright

vernal canyon
#

also u can do this

honest flume
#

after the inputs

vernal canyon
#

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

honest flume
#

i was just trying to understand the other one...

vernal canyon
#

u can put the member name inside it as well if u want

#

member = {"number": member_number, "name": member_name}

honest flume
#

ok

#

ok

vernal canyon
#

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)
honest flume
vernal canyon
#

i'm assigning the member to the key member_name of members

honest flume
#

yeah right

vernal canyon
#

that way if i want to access that member

honest flume
#

ok i get it

vernal canyon
#

all i need is his name

honest flume
#

i will still need a few hours to process this information

#

so ima take a rest

vernal canyon
#

ok

honest flume
#

cya

vernal canyon
#

cya

mild birchBOT
#
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.