admin管理员组

文章数量:1307166

name = input("Enter your name: ")
bid = int(input("Enter your bid: "))
bids = {}
for name,bid in bids.items():
    bids += { name : bid }
print(bids)

I am getting empty output as {}.

Could you please help me to rectify my error? As a fellow beginner in Python.

name = input("Enter your name: ")
bid = int(input("Enter your bid: "))
bids = {}
for name,bid in bids.items():
    bids += { name : bid }
print(bids)

I am getting empty output as {}.

Could you please help me to rectify my error? As a fellow beginner in Python.

Share Improve this question edited Feb 3 at 6:48 Mark Tolonen 178k26 gold badges180 silver badges270 bronze badges asked Feb 3 at 6:40 veluguri praveenveluguri praveen 11 bronze badge 4
  • 4 for name,bid in bids.items():bids is empty, so that loop won’t do anything. You only have one item you want to append anyway, no need for a loop at all. – deceze Commented Feb 3 at 6:42
  • 2 You also can't += dictionaries. Use bids[name] = bid for a single key/value addition or bids.update(other_dict) to add keys from another dictionary. – Mark Tolonen Commented Feb 3 at 6:51
  • What is the purpose of a dictionary in this case? – Adon Bilivit Commented Feb 3 at 6:51
  • You don't need the loop at all. Just assign bids = {name: bid} and you're done. – John Gordon Commented Feb 3 at 17:23
Add a comment  | 

2 Answers 2

Reset to default 1

If you want a dictionary to store your name and bid values then it's just:

name = input("Enter your name: ")
bid = int(input("Enter your bid: "))
bids = {name: bid}
print(bids)

There is no need for loop, try this easy method, but there are others too:

name=input('Enter your name: ')
bid=input('Enter your bid: ')
dict1={}
dict1[name]=bid
print(dict1)

本文标签: Python Dictionary for my name and bid programStack Overflow