import random
data_bid = {}
n = 2
uniq_no = random.randint(101, 1000)
print(f"Bid NO. :{uniq_no} ")
name = input("Name: ")
phone = input("Phone Number: ")
while len(phone) != 10:
print("<<<<<<-----------Please Enter Valid Data---------->>>>>>")
phone = input("Phone Number: ")
bid_amount = float(input("Enter The Bid amount $: "))
for num in range(1, n):
data_bid[f"{uniq_no}"]["name"] = name
data_bid[f"{uniq_no}"]["phone"] = phone
data_bid[f"{uniq_no}"]["bid amount"] = bid_amount
when I run the code I get KeyError: '553'
I'm unable to resolve it and I want to store my data
data_bid = {"uniq_no" ={"name" = name, "phone" = phone}}
I want to store data in a nested dictionary.
CodePudding user response:
In order to have a nested dictionary, you need each new uniq_no
to have a dictionary initialized as it's value. You can achive this by initializing a dictionary for each new uniq_no
:
data_bid[uniq_no] = {}
However, the pythonic way to do this is by using defaultdict
:
import random
from collections import defaultdict
data_bid = defaultdict(dict)
n = 2
uniq_no = random.randint(101, 1000)
print(f"Bid NO. :{uniq_no} ")
name = input("Name: ")
phone = input("Phone Number: ")
while len(phone) != 10:
print("<<<<<<-----------Please Enter Valid Data---------->>>>>>")
phone = input("Phone Number: ")
bid_amount = float(input("Enter The Bid amount $: "))
for num in range(1, n):
data_bid[uniq_no]["name"] = name
data_bid[uniq_no]["phone"] = phone
data_bid[uniq_no]["bid amount"] = bid_amount
Quoting python's documentation:
A
defaultdict
returns a new dictionary-like object.defaultdict
is a subclass of the built-indict
class. It overrides one method and adds one writable instance variable. The remaining functionality is the same as for thedict
.
In addition, I didn't understand why setting the dictionary key as a string. You can use the original integer value.
CodePudding user response:
I agree with @eyal-golan and would probably use collections.defaultdict(dict)
myself, I thought though that I would show a simple alternative using setdefault()
. It is a one line addition to your current code:
import random
data_bid = {}
n = 2
uniq_no = random.randint(101, 1000)
print(f"Bid NO. :{uniq_no} ")
name = input("Name: ")
phone = input("Phone Number: ")
while len(phone) != 10:
print("<<<<<<-----------Please Enter Valid Data---------->>>>>>")
phone = input("Phone Number: ")
bid_amount = float(input("Enter The Bid amount $: "))
## ---------------------------
## if this key is not in the dictionary add it with a value of an
## empty dictionary.
## ---------------------------
data_bid.setdefault(f"{uniq_no}", {})
## ---------------------------
for num in range(1, n):
data_bid[f"{uniq_no}"]["name"] = name
data_bid[f"{uniq_no}"]["phone"] = phone
data_bid[f"{uniq_no}"]["bid amount"] = bid_amount
print(data_bid)
CodePudding user response:
You need to initialize your nested dictionary too. Otherwise, when you try to set a value on the nested dictionary it will throw an error.
I'm not sure why are you setting the dictionary key as string. Did you notice that you can use the integer directly?
import random
data_bid = {}
n = 2
uniq_no = random.randint(101, 1000)
data_bid[uniq_no] = {} # create the nested dictionary
print(f"Bid NO. :{uniq_no} ")
name = input("Name: ")
phone = input("Phone Number: ")
while len(phone) != 10:
print("<<<<<<-----------Please Enter Valid Data---------->>>>>>")
phone = input("Phone Number: ")
bid_amount = float(input("Enter The Bid amount $: "))
for num in range(1, n):
data_bid[uniq_no]["name"] = name # Save the values on the nested dictionary
data_bid[uniq_no]["phone"] = phone
data_bid[uniq_no]["bid amount"] = bid_amount
Of course, you can keep the f"{uniq_no}"
and use an string instead a int
...
data_bid[f"{uniq_no}"] = {}
...
for num in range(1, n):
data_bid[f"{uniq_no}"]["name"] = name
data_bid[f"{uniq_no}"]["phone"] = phone
data_bid[f"{uniq_no}"]["bid amount"] = bid_amount