Tell us what’s happening:
Sorry for the large confused block of code.
I have several problems:
-
I don’t understand why my lower_case(dictionary) function breaks the validation of making the key lower case. I’m not actually sure which key it’s telling me to change the case of, I assumed it was the argument-key being passed to the function so I don’t know why my changing of the original test dictionary would make a difference.
-
I get a Keyerror when attempting to delete either my beginning settings, Theme or Notifications, or some variation of the text, ie. the or them. But, if I update the settings then it all seems to work. I tried del dictionary[key] which didn’t work. I tried the .get() method but that didn’t work either.
-
Depending on how I toggle my settings and change the str() function and .lower() method I either go all the way to the beginning with “add setting should change the key to lower case” or to the current problem of producing a Keyerror and/or simply unable to match the desired key to the key_set.
-
Now I’m completely lost in something that I’ve done because I’m back to failing the first validation of changing the add setting key to lower case when all of my function calls output correctly. Owell, off to work.
Your code so far
test_settings = {"THEME": "Light", "notifiCations": "enaBled"}
print(f"{test_settings}\n\n")
def add_setting(dictionary, settings):
#dictionary = lower_case(dictionary)
key = settings[0].lower()
value = settings[1].lower()
key_set = str(dictionary.keys()).lower()
if key not in key_set:
#if str(key) not in str(key_set):
#if str(key).lower() not in str(key_set).lower():
return f"Setting '{key}' already exists! Cannot add a new setting with this name."
dictionary[key] = value
print(f"{view_settings(dictionary)}\n")
return f"Setting '{key}' added with value '{value}' successfully!"
def update_setting(dictionary, settings):
#dictionary = lower_case(dictionary)
key = settings[0].lower()
value = settings[1].lower()
key_set = str(dictionary.keys()).lower()
if key not in key_set:
#if str(key) not in str(key_set):
#if str(key).lower() not in str(key_set).lower():
return f"Setting '{key}' does not exist! Cannot update a non-existing setting."
dictionary[key] = value
print(f"{view_settings(dictionary)}\n")
return f"Setting '{key}' updated to '{value}' successfully!"
def delete_setting(dictionary, settings):
key = settings[0].lower()
value = settings[1].lower()
key_set = str(dictionary.keys()).lower()
#print(f"key set: {key_set}\n")
#dictionary = lower_case(dictionary)
#print(f"key change: {key}\n")
#print(f"dictionary: {dictionary}\n")
if key not in key_set:
#print(f"{view_settings(dictionary)}\n")
return f"Setting not found!"
dictionary.pop(key)
#print(f"{view_settings(dictionary)}\n")
return f"Setting '{key}' deleted successfully!"
def view_settings(dictionary):
dictionary = lower_case(dictionary)
if not dictionary:
return "No settings available."
return f"\nCurrent User Settings: {dictionary}"
def lower_case(dictionary):
lowercase_dictionary = {}
for key, value in dictionary.items():
lowercase_dictionary[key.lower()] = value
for key,value in lowercase_dictionary.items():
lowercase_dictionary[key] = value.lower()
dictionary = lowercase_dictionary
return dictionary
print(f"{add_setting((test_settings), ('Volume', 'High'))}\n")
#print(f"{add_setting((test_settings), ('Theme', 'Light'))}\n")
print(f"{update_setting(test_settings, ('Theme', 'Dark'))}\n")
print(f"{update_setting(test_settings, ('Screen', 'Large'))}\n")
print(f"{update_setting(test_settings, ('Notifications', 'Disable'))}\n")
print(f"{delete_setting(test_settings, ('volume', 'Low'))}\n")
print(f"{delete_setting(test_settings, ('Theme', 'Dark'))}\n")
print(f"{delete_setting(test_settings, ('Notifications', 'Enabled'))}\n")
print(f"{view_settings(test_settings)}\n")
Lesson URL (copy - paste from your browser’s address bar)