Author Climent
Author Climent

Reputation: 83

Add value to the key in the list PYTHON

I have a variable which ask to add new name which will be add to the list. But it's not working if the list has {} except of this []. To [] i can't add a value, because it gives me an error. I want to add value and key to the list, from the variables.

users={"Peter": 600, "Georgo": 700, "Mike": 800}

def add_user():
    new=str(input("Zadaj meno nového zamestnanca: "))
    users.append(new)
    print(users)

Upvotes: 0

Views: 59

Answers (1)

snowneji
snowneji

Reputation: 1136

If you're using a dictionary, you cannot append to it, but you can do the following:

users={"Peter": 600, "Georgo": 700, "Mike": 800}

def add_user():
     new_user=str(input("Zadaj meno nového zamestnanca: "))
     new_number=str(input("input a number:"))

     users[new_user] = new_number

     print(users)

Upvotes: 3

Related Questions