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
#🔒 Adding a second value to the dictionary... is it as simple as a variable change?
170 messages · Page 1 of 1 (latest)
@fiery wigeon
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.
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
I think i am overcomplicating it
the only way I know of adding an item into a dictionary is
some_dict[keys] = value
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
why are you updating contact when it's being iterated?
use a different variable
like temp_dict?
yeah something like that
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...
unique_id = 3
temp_dict[unique_id] = 10
contact = {1:2}
contact = dict(temp_dict[unique_id])
but then we can create temp_dict instanteously through contact
!e
unique_id = 3
temp_dict = {}
temp_dict[unique_id] = 10
contact = {1:2}
contact = dict(temp_dict[unique_id])
@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
the point of temp_dict is that you con't change contact
at least not until you finished with the for loop
or the function
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
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
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
what's your goal?
after the user creates a contact, what do you want to do with the info?
save it into a file?
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
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
dictionary doesnt take append.
you got like 5 or more fields you're trying to add
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
?
Yeah peanut
Can u change ur name to American Peanuts
Americanized Peanut
Something like this
idk i just told chatgpt to make this avatar lol
dict itself doesn't create a dictionary. you need to call it like a function. create_contact(dict) => create_contact(dict())
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)
you can't access a dictionary via index directly
I dont understand how the teach just immediately created a dictionary that appended values
dictionaries dont have indexes
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)
@fiery wigeon :white_check_mark: Your 3.12 eval job has completed with return code 0.
{1: {'name': 'Peanut'}}
MMM....
the name of his dictionary is 1
yes thats the key
no, name is the key
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
the python bot says:
{1: {'name': 'Peanut'}}
1 - the name of the dictionary
name - key
Peanut - value
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)
I interpret it as 1: is the key: and name: name is the values
brb in 5
it's a nested dict, so 1 is a key and "name" is a key in the nested dictionary
ok so 1 set of keyand vlaue is 1 and {name:peanut} or would it be better to represent it as 1 and [name:peanut]..... the other is name: peanut
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
@fiery wigeon https://paste.pythondiscord.com/2WGQ
the one that I made checks if the person's name already exists before adding it to .json file
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
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?
maybe try reading this article or watching this video? https://realpython.com/python-dicts/ and https://www.youtube.com/watch?v=daefaLgNkw0
watching 🍿
i just wanna make a dictionary longer than 1 ...
i assume you mean this?
!e ```py
contacts = {}
contacts["a"] = 1
contacts["b"] = 2
print(contacts)
@stable spruce :white_check_mark: Your 3.12 eval job has completed with return code 0.
{'a': 1, 'b': 2}
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...
all items are unique in a dictionary
All keys must be unique in a dictionary. A dictionary may have duplicate values.
they can have the same value but not the same key
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)
@fiery wigeon :white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | name John
002 | telephone 0404324
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
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?
do i need to call twice maybe
no I need multiple keys...
with 2 function calls...
your key is unique every time you call the function
you want this
foo = {}
create_contact(foo)
create_contact(foo)
yes if you want to abuse the mutable default parameter you need to call it without arguments
i dont understand
u mean with arguments?
(create_contact(contact))
^^^^^^^
NameError: name 'contact' is not defined
no, create_contact() would be calling without arguments
create_contact(contact) is calling with 1 argument
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))
contact= {unique_id : [name, organisation, address, empty_phone_list]} is the bad line
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
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.