英文:
How can i add new key and value in a existing list
问题
Here is the translated code part:
import os
data_base = []
while True:
name = input("What is your name?: ")
bid = input("What is your bid: $")
other_user = input("Are there any other bidders? Type 'yes' or 'no'.\n")
if other_user == 'yes':
os.system('cls')
def new_user(name, bid):
brandnew_user = {
name: [bid]
}
data_base.append(brandnew_user)
new_user(name, bid)
print(data_base)
if other_user == 'no':
break
I've removed unnecessary characters and made some corrections to the code.
英文:
I'm trying to create a blind auction. So basically it will ask for your name and then your bid. After that, it will ask if there are any bidders, if yes it will ask you the name and the bid price. But after you said yes the terminal will be cleaned. So that the other bidder can't see how much the other person bid, if I run print on the [data_base] it can't print more than two keys and value.
Here is the output:
What is your name?: Gael
What is your bid: $560
Are there any other bidders? Type 'yes or 'no'.
yes
\[({'Gael': \['560'\]},)\]
What is your name?: Mikey
What is your bid: $350
Are there any other bidders? Type 'yes or 'no'.
yes
\[({'Mikey': \['350'\]},)\]
What is your name?: Josh
What is your bid: $298
Are there any other bidders? Type 'yes or 'no'.
no
Here is the final output:
[({'Mikey': ['350']},), ({'Josh': ['298']},)]
Gael's name and his bid are missing.
Here is the code:
import os
while True:
name = input("What is your name?: ")
bid = input("What is your bid: $")
other_user = input("Are there any other bidders? Type 'yes or 'no'.\n")
if other_user == 'yes':
os.system('cls')
data_base = [
]
def new_user(name, bid):
brandnew_user = {
name: [bid]
},
data_base.append(brandnew_user)
new_user(name, bid)
print(data_base)
if other_user == 'no':
break
Thank you!!
I was expecting that Gael's name and bid will be recorded. But it did not, it only recorded, Mikey and Josh.
答案1
得分: 1
这是一个更好的组织方式。此外,我不确定您为什么要创建一个元组列表的字典。为什么不只是将 data_base
设为字典,并将新条目存储为键?
import os
data_base = []
while True:
name = input("What is your name?: ")
bid = input("What is your bid: $")
data_base.append({name: [bid]})
print(data_base)
other_user = input("Are there any other bidders? Type 'yes or 'no'.\n")
if other_user == 'no':
break
这是我所说的方式:
import os
data_base = {}
while True:
name = input("What is your name?: ")
bid = input("What is your bid: $")
data_base[name] = [bid]
print(data_base)
other_user = input("Are there any other bidders? Type 'yes or 'no'.\n")
if other_user == 'no':
break
英文:
Here's a better way to organize things. Also, I'm not sure why you are creating a list of tuples of dictionaries. Why not just make data_base
a dictionary and store the new entries as keys?
import os
data_base = []
while True:
name = input("What is your name?: ")
bid = input("What is your bid: $")
data_base.append( {name: [bid]} )
print(data_base)
other_user = input("Are there any other bidders? Type 'yes or 'no'.\n")
if other_user == 'no':
break
Here's what I'm talking about:
import os
data_base = {}
while True:
name = input("What is your name?: ")
bid = input("What is your bid: $")
data_base[name] = [bid]
print(data_base)
other_user = input("Are there any other bidders? Type 'yes or 'no'.\n")
if other_user == 'no':
break
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论