#🔒 Adding a second value to the dictionary... is it as simple as a variable change?

170 messages · Page 1 of 1 (latest)

fiery wigeon
#
def create_contact(contact={}):
    name = input("Your name?")
    organisation = input("Your organisation?")
    address = input("Ur address?")
    phone_numbers = (input("Your phone numbers"))
    unique_id = secrets.randbelow(99999)
    second_number = (input("Your phone numbers, residential?"))
    empty_phone_list = []
    # if unique_id in contact:
    # return contact

    # if unique_id not in contact:
    if name != "" and organisation != "" and address != "" and phone_numbers != "":
        contact = {unique_id: [name, organisation, address,[]] }
        if second_number != '':
            while True:
                check = input("Do you want to add more phone numbers? y/n")
                if check in "Nn":
                    empty_phone_list.append(phone_numbers)
                    empty_phone_list.append(second_number)
                    contact[unique_id][3] = empty_phone_list
                    break
                elif check in "Yy":
                    third_number = input("Final call for phone numbers?")
                    empty_phone_list.append(third_number)
                    contact[unique_id][3] = empty_phone_list
                else:
                    print("This variable is compromised")
                    pass

                #            for key in contact:
            contact = contact[unique_id] #this basically calls the list of values... except the unique_id...
            print(unique_id, contact) # so then printing can be call inside a for loop? for unique_id is                   unique
            return contact
weary novaBOT
#

@fiery wigeon

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.

fiery wigeon
#

So there is a larger dictionary I must be creating... in which all my contacts are stored...

#

Currently the code just overwrites the 1st inputted data.

#
            for unique_id in contact:
                contact = contact[unique_id]
                print(unique_id, contact)
                unique_list_of_id = []
                unique_list_of_id.append(unique_id)
            return contact
#
            unique_list_of_id = []
            unique_list_of_id.append(unique_id)
            for k in len(unique_list_of_id):
                contact = contact[unique_id]
                print(unique_id, contact)
            return contact
#

for k in len(unique_list_of_id):
TypeError: 'int' object is not

fiery wigeon
#

I think i am overcomplicating it

peak knoll
#

the only way I know of adding an item into a dictionary is

some_dict[keys] = value
fiery wigeon
#

might just need a update

#

after

#

gimme 5 my pycharm is booting

#

im thinking contact = contact[unique_id] /... this prints the values after the unique id

#

but its also the key of the dictio0nary

peak knoll
#

why are you updating contact when it's being iterated?

fiery wigeon
#

forget the iteration

#

that was a mistake

peak knoll
#

use a different variable

fiery wigeon
#

like temp_dict?

peak knoll
#

yeah something like that

fiery wigeon
#

pycharm wont open

#

=

#

contact = dict(temp_dict[unique_id])

#

whats the point of the temp_dict...

#

ah right i see

#

its like where putting all these values step-wise into contact...

peak knoll
fiery wigeon
#

but then we can create temp_dict instanteously through contact

peak knoll
#

!e

unique_id = 3
temp_dict = {}
temp_dict[unique_id] = 10
contact = {1:2}
contact = dict(temp_dict[unique_id])
weary novaBOT
#

@peak knoll :x: Your 3.12 eval job has completed with return code 1.

001 | Traceback (most recent call last):
002 |   File "/home/main.py", line 5, in <module>
003 |     contact = dict(temp_dict[unique_id])
004 |               ^^^^^^^^^^^^^^^^^^^^^^^^^^
005 | TypeError: 'int' object is not iterable
peak knoll
#

the point of temp_dict is that you con't change contact

#

at least not until you finished with the for loop

fiery wigeon
#
                    contact = contact[unique_id]
                    print(unique_id, contact)
#

current iteration... yields the desired result for one user

#

2nd iteration...probabilistically must save the 1st iteration... i.e not overwrite that

peak knoll
#

in any case you probably shouldn't be changing your contact that is being iterated

#

the temp_dict is there to save any information that will be modified into contact when the loop finishes i.e. contact is no longer being iterated

fiery wigeon
#
def create_contact(contact={}):
    name = input("Your name?")
    organisation = input("Your organisation?")
    address = input("Ur address?")
    phone_numbers = (input("Your phone numbers"))
    unique_id = secrets.randbelow(99999)
    second_number = (input("Your phone numbers, residential?"))
    empty_phone_list = []
    # if unique_id in contact:
    # return contact

    # if unique_id not in contact:
    if name != "" and organisation != "" and address != "" and phone_numbers != "":
        if second_number != '':
            while True:
                check = input("Do you want to add more phone numbers? y/n")
                if check in "Nn":
                    empty_phone_list.append(phone_numbers)
                    empty_phone_list.append(second_number)
                    break
                elif check in "Yy":
                    third_number = input("Final call for phone numbers?")
                    empty_phone_list.append(third_number)
                else:
                    print("This variable is compromised")

                #            for key in contact:
            contact[unique_id] = [name, organisation, address, empty_phone_list]

            return contact


(create_contact(1))
print(contact)
#

not sure why this is happening

#

myprofessuer seemed to get awy with this....

#
import secrets


def create_contact(contact={}):

    name = input("Your name?")
    organisation = input("Your organisation?")
    address = input("Ur address?")
    phone_numbers = (input("Your phone numbers"))
    unique_id = secrets.randbelow(99999)
    while unique_id in contact.keys():
        unique_id= secrets.randbelow(99999)
    second_number = (input("Your phone numbers, residential?"))
    empty_phone_list = []
    # if unique_id in contact:
    # return contact

    # if unique_id not in contact:
    if name != "" and organisation != "" and address != "" and phone_numbers != "":
        if second_number != '':
            while True:
                check = input("Do you want to add more phone numbers? y/n")
                if check in "Nn":
                    empty_phone_list.append(phone_numbers)
                    empty_phone_list.append(second_number)
                    break
                elif check in "Yy":
                    third_number = input("Final call for phone numbers?")
                    empty_phone_list.append(third_number)
                else:
                    print("This variable is compromised")

                #            for key in contact:
    contact[unique_id] = [name, organisation, address, empty_phone_list]

    return contact


(create_contact(1))
print(contact)

#

So there is a larger dictionary I must be creating... in which all my contacts are stored...
Currently the code just overwrites the 1st inputted data.

#

Albeit a few errors

mystic fossil
#

what's your goal?

#

after the user creates a contact, what do you want to do with the info?

#

save it into a file?

fiery wigeon
#

import random
import secrets
def create_contact(contact={}):

        name = input("Your name?")
        organisation = input("Your organisation?")
        address = input("Ur address?")
        phone_numbers = (input("Your phone numbers"))
        unique_id = secrets.randbelow(99999)
        second_number = (input("Your phone numbers, residential?"))
        empty_phone_list = []
       # if unique_id in contact:
               # return contact
            
        #if unique_id not in contact:
        if name !="" and organisation != "" and address != "" and phone_numbers != "":
                if second_number != '':
                 while True:
                    third_number = input("Final call for phone numbers?")
                    check = input("Do you want to add more phone numbers? y/n")
                    if check in "Nn":
                        empty_phone_list.append(phone_numbers)
                        empty_phone_list.append(second_number)
                        break
                    elif check in "Yy":
                        empty_phone_list.append(third_number)                        
                    else:
                         print("Sizzling Smog")

#            for key in contact: 
                contact[unique_id] =  [name, organisation, address, empty_phone_list]
                print(empty_phone_list)
                return contact
(create_contact(dict))
#

No. I want to keep appending to the dictionary with unique IDs

#

{111111: ["Tom", "Melbourne", "Monash", ["0412345678", "0498765432"]],
2222222: ["Jerry", "Carnegie", "Youtube", ["0412345879"]]}
"""

#

See this example... 2 people stored in dictionary

#

currently i'm limited to 1

#

contact[unique_id] = [name, organisation, address, empty_phone_list]
~~~~~~~^^^^^^^^^^^
TypeError: 'type' object does not support item assignment

mystic fossil
#

a dictionary isn't a permanent storage

#

to add another item to a dictionary, you use the append method

#

this wouldn't be a dictionary anyway

fiery wigeon
#

dictionary doesnt take append.

mystic fossil
#

you got like 5 or more fields you're trying to add

fiery wigeon
#

I kind of need a temporary dictionary.. whereby I get the first key from the dictionary... then add 5 values... iterate again but this time second key from the dictionary...then add the corresponding 5values

#

?

mystic fossil
#

you probably want to use a list of lists

#

a dictionary is only for key:value pairs

fiery wigeon
#

Yeah peanut

#

Can u change ur name to American Peanuts

#

Americanized Peanut

#

Something like this

mystic fossil
#

idk i just told chatgpt to make this avatar lol

fiery wigeon
#

If u go settings

#

Profile u can change ur name

stable spruce
#

dict itself doesn't create a dictionary. you need to call it like a function. create_contact(dict) => create_contact(dict())

fiery wigeon
#
import random
import secrets
def create_contact(contact={}):

        name = input("Your name?")
        organisation = input("Your organisation?")
        address = input("Ur address?")
        phone_numbers = (input("Your phone numbers"))
        unique_id = secrets.randbelow(99999)
        second_number = (input("Your phone numbers, residential?"))
        empty_phone_list = []
       # if unique_id in contact:
               # return contact
            
        #if unique_id not in contact:
        if name !="" and organisation != "" and address != "" and phone_numbers != "":
                if second_number != '':
                 while True:
                    third_number = input("Final call for phone numbers?")
                    check = input("Do you want to add more phone numbers? y/n")
                    if check in "Nn":
                        empty_phone_list.append(phone_numbers)
                        empty_phone_list.append(second_number)
                        break
                    elif check in "Yy":
                        empty_phone_list.append(third_number)                        
                    else:
                         print("Sizzling Smog")

#            for key in contact: 
                contact= {unique_id : [name, organisation, address, empty_phone_list]}
                for k, v in contact.items(): 
                    if k not in contact.items():
                        contact[unique_id].append(v)
                        print(k, v)
                   # temp_dict[k] = {}

                print(contact)
                return contact
(create_contact())
#

78886 ['Eug', '3wq432', '234', ['32324', '43'], [...]]
{78886: ['Eug', '3wq432', '234', ['32324', '43'], [...]]}

#

!e

#

!e `py

#

!e ```py
contact = {1: {"name": "Peanut"}}
temp_dict = (contact[1])
for k, v in contact.items():
temp_dict.append(v)
print(contact)

peak knoll
#

you can't access a dictionary via index directly

fiery wigeon
#

I dont understand how the teach just immediately created a dictionary that appended values

mystic fossil
#

dictionaries dont have indexes

fiery wigeon
#

she like only had a return statement

#

!e ```py
contact = {1: {"name": "Peanut"}}
temp_dict = (contact[1])
for k, v in contact.items():
temp_dict.update(v)
print(contact)

weary novaBOT
#

@fiery wigeon :white_check_mark: Your 3.12 eval job has completed with return code 0.

{1: {'name': 'Peanut'}}
fiery wigeon
#

MMM....

mystic fossil
#

the name of his dictionary is 1

fiery wigeon
#

yes thats the key

mystic fossil
#

no, name is the key

fiery wigeon
#

but his name is contact_dictionary...

#

but i literally have the same code

#

like theres no difference

#

if u will indulge me in a screenshot

mystic fossil
#

the python bot says:
{1: {'name': 'Peanut'}}

1 - the name of the dictionary
name - key
Peanut - value

fiery wigeon
#
import secrets


def create_contact(contact={}):
    name = input("Your name?")
    organisation = input("Your organisation?")
    address = input("Ur address?")
    phone_numbers = (input("Your phone numbers"))
    unique_id = secrets.randbelow(99999)
    while unique_id in contact.keys():
        unique_id= secrets.randbelow(99999)
    second_number = (input("Your phone numbers, residential?"))
    empty_phone_list = []
    # if unique_id in contact:
    # return contact

    # if unique_id not in contact:
    if name != "" and organisation != "" and address != "" and phone_numbers != "":
        if second_number != '':
            while True:
                check = input("Do you want to add more phone numbers? y/n")
                if check in "Nn":
                    empty_phone_list.append(phone_numbers)
                    empty_phone_list.append(second_number)
                    break
                elif check in "Yy":
                    third_number = input("Final call for phone numbers?")
                    empty_phone_list.append(third_number)
                else:
                    print("This variable is compromised")

                #            for key in contact:
    contact[unique_id] = [name, organisation, address, empty_phone_list]
    return contact
(create_contact(1))
print(contact)
fiery wigeon
#

brb in 5

stable spruce
#

it's a nested dict, so 1 is a key and "name" is a key in the nested dictionary

fiery wigeon
stable spruce
#

does your code not crash? your last two lines look wrong

#

also your code is very different from that screenshot. i'm not sure what you mean by that

mystic fossil
fiery wigeon
#

@mystic fossil

#

@stable spruce

mystic fossil
#

the one that I made checks if the person's name already exists before adding it to .json file

fiery wigeon
#

mmm

#

i just had a general perousal of that

#

if i enumerate through the keys and values

#

and then have a separate i counter

#

i+=1 for every loop...

#

if i == dlksafd

stable spruce
#

i'm confused what you're trying to do. why are you appending to the array, and why are you looping through the keys in contact?

fiery wigeon
#

watching 🍿

fiery wigeon
stable spruce
#

i assume you mean this?

#

!e ```py
contacts = {}
contacts["a"] = 1
contacts["b"] = 2
print(contacts)

weary novaBOT
#

@stable spruce :white_check_mark: Your 3.12 eval job has completed with return code 0.

{'a': 1, 'b': 2}
fiery wigeon
#

yeah

#

so in this case "a"and "b"are gonna be unique_ids

#

"a" initialised through the 1st run through

#

"b" initliased through the 2nd run...

mystic fossil
#

all items are unique in a dictionary

dense raptor
#

All keys must be unique in a dictionary. A dictionary may have duplicate values.

mystic fossil
#

they can have the same value but not the same key

fiery wigeon
#
               contact= {unique_id : [name, organisation, address, empty_phone_list]}
                for k, v in contact.items(): 
                    #if k not in contact.items():
                        #temp_dict = contact[unique_id]
                        print(k, v)
                   # temp_dict[k] = {}
                jason = {"name" : "John", "telephone": "0404324"}
                for k, v in jason.items():
                    print(k,v)
#

I think the answer is here.

#

!e ```py
jason = {"name" : "John", "telephone": "0404324"}
for k, v in jason.items():
print(k,v)

weary novaBOT
#

@fiery wigeon :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | name John
002 | telephone 0404324
fiery wigeon
#

right... but then why dont i get this in my output for k,v contact.items

#

4432432: John

#

4432432 Caritas

#

4432432 The Moon

#

4432432 94932432, 9324932432

#

91594 ['as', 'as', 'sas', ['sas', 'sas']]

#

bvrb eating chicken

stable spruce
#

if you want to have your dictionary have multiple keys, i don't really see you trying to do that

#

are you trying to call create_contact multiple times?

fiery wigeon
fiery wigeon
#

with 2 function calls...

stable spruce
#

your key is unique every time you call the function

#

you want this

#
foo = {}
create_contact(foo)
create_contact(foo)
fiery wigeon
#

i got that tho

#
def create_contact(contact={}):

#

i call it create_contact()

stable spruce
#

yes if you want to abuse the mutable default parameter you need to call it without arguments

fiery wigeon
#

i dont understand

#

u mean with arguments?

#

(create_contact(contact))
^^^^^^^
NameError: name 'contact' is not defined

stable spruce
#

no, create_contact() would be calling without arguments

#

create_contact(contact) is calling with 1 argument

fiery wigeon
#

mmm

#

ok

#
contact={}
def create_contact(contact={}):
#

create_contact(contact)

#

makes some sense.

#

if i keep passing in an empty thing at the top it wont work

#
Do you want to add more phone numbers? y/nn
53567 ['Name', 'organisation', 'Address', ['0404210931', '213213']]
['Name', 'organisation', 'Address', ['0404210931', '213213']]
Your name?Name1
Your organisation?Organisation1
Ur address?Address1
Your phone numbers000
Your phone numbers, residential?911
Final call for phone numbers?8
Do you want to add more phone numbers? y/nn
32818 ['Name1', 'Organisation1', 'Address1', ['000', '911']]
['Name1', 'Organisation1', 'Address1', ['000', '911']]
#

Butstill the output remains separate.

#

import random
import secrets
contact={}
def create_contact(contact):

        name = input("Your name?")
        organisation = input("Your organisation?")
        address = input("Ur address?")
        phone_numbers = (input("Your phone numbers"))
        unique_id = secrets.randbelow(99999)
        second_number = (input("Your phone numbers, residential?"))
        empty_phone_list = []
        temp_dict = {}
       # if unique_id in contact:
               # return contact
            
        #if unique_id not in contact:
        if name !="" and organisation != "" and address != "" and phone_numbers != "":
                if second_number != '':
                 while True:
                    third_number = input("Final call for phone numbers?")
                    check = input("Do you want to add more phone numbers? y/n")
                    if check in "Nn":
                        empty_phone_list.append(phone_numbers)
                        empty_phone_list.append(second_number)
                        break
                    elif check in "Yy":
                        empty_phone_list.append(third_number)                        
                    else:
                         print("Invalid")

#            for key in contact: 
                contact= {unique_id : [name, organisation, address, empty_phone_list]}
                print(contact)
                return contact
(create_contact(contact))
(create_contact(contact))
stable spruce
#

contact= {unique_id : [name, organisation, address, empty_phone_list]} is the bad line

fiery wigeon
#

contact[unique_id] = [name, organisation, address, empty_phone_list]?

#

{675: ['aa', 'a', 'a', ['a', 'a']], 33868: ['b', 'b', 'b', ['b', 'b']]}

#

okitwrosk

#

yay

#

"D

#

!close

weary novaBOT
#
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.