Build a User Configuration Manager - Build a User Configuration Manager - Tests 4 & 5

Tell us what’s happening:

The code is failing the tests 4 and 5, but the output is working properly.
From the code I have seen on other related posts this should work. I’m not sure if I’m missing a importante detail or if it should be writen in another way.

Context:
Test 4 - add_setting should convert the key to lowercase.
Test 5 - add_setting should convert the value to lowercase.

Thanks in advance :slight_smile:

Your code so far

test_settings ={'theme': 'light'}

def add_setting (dict_settings, new_setting):
    new_setting_key = new_setting[0].lower()
    new_setting_val = new_setting[1].lower()
    if new_setting_key in set(dict_settings.keys()):
       return f"Setting '{new_setting_key}' already exists! Cannot add a new setting with this name."
    else:
        test_settings[new_setting_key] = new_setting_val
        return f"Setting '{new_setting_key}' added with value '{new_setting_val}' successfully!"

def update_setting (dict_settings, update_setting):
    update_setting_key = str(update_setting[0]).lower()
    update_setting_val = str(update_setting[1]).lower()
    if update_setting_key in set(dict_settings.keys()):
        test_settings[update_setting_key] = update_setting_val
        return f"Setting '{update_setting_key}' updated to '{update_setting_val}' successfully!"
    else:
        return f"Setting '{update_setting_key}' does not exist! Cannot update a non-existing setting." 

def delete_setting (dict_settings, delete_setting_key):
    delete_setting_key = delete_setting_key.lower()
    if delete_setting_key in set(dict_settings.keys()):
        test_settings.pop(delete_setting_key)
        return f"Setting '{delete_setting_key}' deleted successfully!"
    else:
        return f"Setting not found!" 

def view_settings (dict_settings):
    if dict_settings == dict():
        return "No settings available."
    else:
        msg = f"Current User Settings:"
        for k in dict_settings:
            msg += f"\n{k.title()}: {dict_settings[k]}" 
        return msg



print(add_setting({'theme': 'light'}, ('THEME', 'dark')))
print(add_setting({'theme': 'light'}, ('volume', 'high')))
print(update_setting({'theme': 'light'}, ('theme', 'dark')))
print(update_setting({'theme': 'light'}, ('volume', 'high')))
print(delete_setting({'theme': 'light'}, 'theme'))
print(view_settings({'theme': 'dark', 'notifications': 'enabled', 'volume': 'high'}) )


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36

Challenge Information:

Build a User Configuration Manager - Build a User Configuration Manager

Github Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-user-configuration-manager/684aaf9ec670c68d20efd0d0.md at main · freeCodeCamp/freeCodeCamp · GitHub

check the parameters of add_settings function

I checked with the print() and isinstance() functions and it seems right the new_setting_key and new_setting_val are strings in lower case

not I was hinting at… what are the two parameters called?

The tests are solved, thanks, but I have a question

My tought while wrinting the code was that every operation it would be made on the test_settings not in the parameter input of each funtion. Because in my mind the input would be a temporary variable and if I change it when I returned from the function the modification it whould be losted.

Why does it save the change if I don’t return it?

the parameter is a variable that exists only in the function that is true, but when you call the function, you pass in a reference to a dictionary, not a copy

it is important you always use the parameters so that a function is reusable with different objects

about the reference, consider this:

dict_a = {"greeting": "hello", "name": "John"}

dict_b = dict_a

dict_b["greeting"] = "hi"

print(f'{dict_a["greeting"]}, {dict_a["name"]}')

what do you expect is printed here? make a prediction, then test the code

I was expecting the “hello Jonh” but the test showed “hi John”. I got it, now makes sense.
But if the “=” sign create a reference how do you create a copy? With tuples?

no, if you put an object in a tuple, it is still an object. you need to create a copy proper. For example with the copy() method: dict_b = dict_a.copy()