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 |2 Answers
Reset to default 1If 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
版权声明:本文标题:Python Dictionary for my name and bid program - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741834440a2400129.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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+=
dictionaries. Usebids[name] = bid
for a single key/value addition orbids.update(other_dict)
to add keys from another dictionary. – Mark Tolonen Commented Feb 3 at 6:51bids = {name: bid}
and you're done. – John Gordon Commented Feb 3 at 17:23