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

I need to take user input and add it to a dictionary but it keeps replacing the previous key and value instead of adding a new one

here is the function that i have already and i have the user input split into the variables command, name, and ip_address. i know that those pass correctly because i tested it with the three print statements, and when i call the function it prints out the right dictionary key and value, but when i run the function again, it replaces the key and value instead of adding a new one, how do i make it add a new key and value instead of replacing the old one?

def server_create(command, name, ip_address):
    print(name)
    print(command)
    print(ip_address)+
    server_list = {}
    server_list[name] = ip_address

>Solution :

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

As mentioned in the comments, since you set server_list to an empty dictionary each time, that will not work. You would have to do something like this:

server_list = {}

def server_create(command, name, ip_address, s_list):
      s_list[name] = ip_address

And then call it as:

server_create(command, name, ip_address, server_list)
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