简体   繁体   中英

Why am I getting the following error message?

The defined function will return the altered dictionary. The parameter 'teams' is a dictionary and the parameter 'driver' is a string. The key of 'teams' is the driver and its value is the other team member. So I am trying to swap the 'driver' from the input parameter and the other team member:

 def switch(teams, driver):
        temp = teams[driver]
        switch[temp] = driver
        del switch[driver]
        return teams

    print(switch({'Jack':'Jill', 'Romeo':'Juliet', 'Drake':'Josh'}, 'Drake'))

Why am I getting the following error message? Where in the code am I going wrong?

switch[temp] = driver TypeError: 'function' object does not support item assignment

To stay consistent with your method:

  1. Pick the value out of the dictionary that's gonna be the new key
  2. Add the new entry to the dictionary
  3. Remove the old entry
    def switch(teams, driver):
       temp = teams[driver]
       teams[temp] = driver
       del teams[driver]
       return teams

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM