Build a User Configuration Manager - Build a User Configuration Manager

Tell us what’s happening:

Hello, I am working on the “Build a User Configuration Manager” course in the Python Certification curriculum.

I am confused on what I am supposed to be doing with the dictionary parameter.

The instructions say “You should define a function named add_setting with two parameters representing a dictionary of settings and a tuple containing a key-value pair.”

Then they say “add_setting([‘theme’: ‘light’}, (‘THEME’, ‘dark’)) should return the error message Setting ‘theme’ already exists! Cannot add a new setting with this name.”

and

“add_setting({‘theme’: ‘light’}, (‘volume’, ‘high’)) should add a new key-value pair and return the success message Setting ‘volume’ added with value ‘high’ successfully!”

My code can add the key_value parameter to the test_settings dictionary and I can return the appropriate message but I do not know what I am supposed to be doing with the dictionary parameter.

Can someone please explain what I should be trying to do with this dictionary parameter? It looks like the tuple parameter is supposed to be for the purpose of being added to the test_settings dictionary. However, I do not find the instructions to be clearly communicating the purpose of the dictionary parameter.

Thank you .

Your code so far

test_settings = {
'Theme': 'dark',
'Notifications': 'enabled',
'Volume': 'high'  
}



def add_setting(Dictionary, key_value):
    
    key = key_value[0].lower()
    value = key_value[1].lower()
    dictionary_keys = test_settings.keys()

    if key in dictionary_keys:
        print(f'Setting {key} already exists! Cannot add a new setting with this name')
    else:
        test_settings[key] = value
        print(f'Setting {key} added with value {value} successfully!')
    
    print(test_settings)

add_setting({'theme': 'light'}, ('volume', 'high'))


Your browser information:

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

Challenge Information:

Build a User Configuration Manager - Build a User Configuration Manager

The first parameter of the add_setting function should be a dictionary to which setting is added. add_setting should not use directly the dictionary defined in the global scope, but instead operate on dictionary that’s passed to it.

Hello, thank you for the reply. I may not be understanding what you are saying correctly. Below is my code so far. It functionally does steps 1 through 8 but does not satisfy steps 6 and 7 despite operating as described.

Are you saying that the dictionary parameter in the function should be adding the input to another dictionary that is separate from the test_settings one?

test_settings = {

‘Theme’: ‘dark’,

‘Notifications’: ‘enabled’,

‘Volume’: ‘high’
}

def add_setting(Dictionary, key_value):
Added_Dictionary = Dictionary

key = key_value[0].lower()

value = key_value[1].lower()

dictionary_keys = Added_Dictionary.keys()



if key in dictionary_keys:

    print(f"Setting '{key}' already exists! Cannot add a new setting with this name.")

else:

    Added_Dictionary\[key\] = value

    print(f"Setting '{key}' added with value '{value}' successfully!")

print(Added_Dictionary)
add_setting({‘theme’: ‘light’}, (‘volume’, ‘high’))

Inside of the function, there should be no operations on the test_settings from the global scope. Any operations should be made on the dictionary that’s passed to the function as first parameter.

What add_settings is returning?

Thank you for responding. Add_setting is returning below.

That’s what it prints, but what is returned by the function?

I see I forgot a return statement. Are you wanting me to write something like below?

def add_setting(Dictionary, key_value):
    Added_Dictionary = Dictionary

    key = key_value[0].lower()

    value = key_value[1].lower()

    dictionary_keys = Added_Dictionary.keys()



    if key in dictionary_keys:

        print(f"Setting '{key}' already exists! Cannot add a new setting with this name.")

    else:
        Added_Dictionary[key] = value
        print(f"Setting '{key}' added with value '{value}' successfully!")

    print(Added_Dictionary)
    return Added_Dictionary

print(add_setting({'theme': 'light'}, ('volume', 'high')))

Yes, however each separate result will need it own return. There are not expectations from the add_setting to print anything on it own. When function doesn’t return anything explicitly, with the return, it implicitly returns None.

Thank you, I was able to get steps 1 through 8 to pass. Am I supposed to be adding both of these to a blank dictionary like below?

test_settings = {
'Theme': 'dark',
'Notifications': 'enabled',
'Volume': 'high'  
}


current_settings = {

}

def add_setting(Dictionary, key_value):
    
    global current_settings
    current_settings = Dictionary
    key = key_value[0].lower()
    value = key_value[1].lower()

    if key in current_settings:
        return(f"Setting '{key}' already exists! Cannot add a new setting with this name.")
    else:
        current_settings[key] = value
        return(f"Setting '{key}' added with value '{value}' successfully!")

print(add_setting({'theme': 'light'}, ('volume', 'high')))

print(current_settings)

You don’t need using global, or reassigning current_settings inside of function. Inside of function, make all necessary operations on the first parameter.

Thank you, I have that part figured out now. I am having an issue with the view_settings function. I don’t know if I can be helped with that in here or if I need to log a new question since this is separate.

Below is my code.

def view_settings(settings):
    global current_settings
    settings_var = settings
    items_view = settings.items()
    print(items_view)

    if settings == {}:
        return "No settings available."
    else:
        Current_settings_text = "Current User Settings:\n"
    #print(Current_settings_text + str(items_view))
        print(Current_settings_text)
        Test_2 = [key.capitalize() for key, value in settings_var.items()]
        for key, value in items_view:
            print(f"{key.capitalize()}: {value}")
            Test = (f"{key.capitalize()}: {value}")
            #print(Test)
            #print(Test)

    return Current_settings_text + Test
    

print(view_settings({'theme': 'dark', 'notifications': 'enabled', 'volume': 'high'}))

I am supposed to have the output

Current User Settings:
Theme: dark
Notifications: enabled
Volume: high

However, my code either has a space after Current User Settings: or the for loop does not continue after the first key-value pair. Am I on the right track on how to accomplish this or should I be using a completely different syntax?

Thank you.

You are reassigning Test on each iteration of the for loop, so you wind up with just the last key/value pair.

Yes, I did see that. I figured out what I needed to do. Thank you all for your help.