Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can i add new key and value in a existing list

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’]},)]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading