Home > Software design >  I need to take user input and add it to a dictionary but it keeps replacing the previous key and val
I need to take user input and add it to a dictionary but it keeps replacing the previous key and val

Time:12-10

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

CodePudding user response:

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)
  • Related