I am getting what I understand to be the correct output, but am failing tests 13 and 27, about updating and viewing.
Your code so far
test_settings = dict() # creats an empty dictionary
set_dict= dict()
def tupple_to_dict(set_tup): #conerts a setting tupple to a list of lowercase values and a 1 item dictionary. Returns list and dictionry
set_list = list(set_tup) #convert tuple to list
#print(f'set_list at line 9 = {set_list}')
for i in [0,1]:(set_list[i])= set_list[i].lower() #convert each element to lower case
#print(f'set_list lower = {set_list}')
for i in range(0, len(set_list), 2): #for i in length of list, with a step interval of 2 so you step over the value in key:value to next key
# print(f'i = {i}')
#print(f'set_list[i] = {set_list[i]} and set_list[i+1] = {set_list[i+1]}')
# print(f'set_dict = {set_dict} before')
set_dict[set_list[i]] = set_list[i + 1] #this is the generic equivalent of the next line
#test_settings['time zone'] = set_list[1]
# print(f'set_dict = {set_dict} after')
return set_list, set_dict
#def add_setting(*args):
#def add_setting(test_settings, set_tup):
def add_setting(settings_dict, set_tup):
# print(test_settings, set_tup)
global test_settings
test_settings = settings_dict # !! Note this is necessary as parameter names can not be global !!
#print(f"test_settings at line 30 = {test_settings}")
set_list, set_dict = tupple_to_dict(set_tup) #Call to function which returns a list and dictionary
if set_list[0] in test_settings: #works
#print(f"set_dict.keys = {set_dict.keys()}")
#if set_dict.keys() in test_settings: #doesn't work
#return(f"Setting {list(set_dict.keys())} already exists! Cannot add a new setting with this name.")
return(f"Setting '{(set_list[0])}' already exists! Cannot add a new setting with this name.")
else:
test_settings.update(set_dict) #update test_settings with the update tuple set_dict
#print(f"test_settings at line 40 = {test_settings}")
return (f"Setting '{set_list[0]}' added with value '{set_list[1]}' successfully!")
# print(f'test_settings = {test_settings} end') print (list(test_settings.keys()))
def update_setting(test_settings, set_tup):
set_list, set_dict = tupple_to_dict(set_tup) #Call to new function which returns a list and dictionary
if set_list[0] in test_settings:
test_settings.update(set_dict)
return (f"Setting '{set_list[0]}' updated to '{set_list[1]}' successfully!")
else:
set_list, set_dict = tupple_to_dict(set_tup) #Call to new function which returns a list and dictionary
#print(f"from line 53, set_list[0] {set_list[0]} ")
return (f"Setting '{set_list[0]}' does not exist! Cannot update a non-existing setting.")
#print(f'test_settings from line 56: {test_settings}')
def delete_setting(test_settings, key):
key = key.lower()
#print(key)
if key in test_settings:
test_settings.pop(key)
return(f"Setting '{key}' deleted successfully!")
else: return(f"Setting not found!")
def view_settings(test_settings):
if test_settings == {}:
return("No settings available.")
else:
return_str = ("\nCurrent User Settings: \n\n")
for item in test_settings.items():
for i in [0,1]: # list [0,1]
if i == 0:
return_str = return_str + item[i].capitalize()
return_str = return_str + ': '
if i == 1:
return_str = return_str + item[i]
return_str = return_str + '\n' + '\n'
return_str = return_str + '\n'
return return_str
#add_setting(test_settings, ('theme', 'DARK'))
#print(add_setting({'theme': 'light'}, ( 'THEME', 'dark')))
#print(add_setting(test_settings,(('theme', 'light'), ( 'THEMEB', 'dark'))))
print(add_setting({'theme': 'light'}, ('volume', 'high')))
#print(f'test_settings from line 89: {test_settings}')
print(update_setting({'theme': 'light'}, ('theme', 'dark')))
print(delete_setting({'theme': 'light'}, ('theme')))
print(delete_setting({'theme': 'light'}, ('time zone')))
print(view_settings({'theme': 'dark', 'notifications': 'enabled', 'volume': 'high'}))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:147.0) Gecko/20100101 Firefox/147.0
Challenge Information:
Build a User Configuration Manager - Build a User Configuration Manager
You’re probably failing those tests due to shared mutable state. Avoid using globals and make sure each function returns fresh dicts. Update/view tests are usually strict about exact formatting (extra spaces or newlines can fail them). Also double-check that keys are normalized consistently.
Thank you! I found the extra spaces in the update function which passed #13. I removed the global and added some content to the dictionary since after removing the global made me fail test 1. It looked to me per the directions that the view output was supposed to be double spaced. Not sure what you mean by returning “fresh dicts” Aren’t I returning formatted strings? I took out the extra \n characters and now “actual” view looks like “expected”, but still fails test #27.
Help Mr. Wizard! (reference to a 1960s cartoon character)
Below is my revised view function…
def view_settings(test_settings):
if test_settings == {}:
return("No settings available.")
else:
return_str = ("\\nCurrent User Settings: \\n")
for item in test_settings.items():
for i in \[0,1\]: # list \[0,1\]
if i == 0:
return_str = return_str + item\[i\].capitalize() + ': '
#return_str = return_str + ': '
if i == 1:
return_str = return_str + item\[i\] + '\\n'
#return_str = return_str + '\\n' + '\\n'
#return_str = return_str + '\\n'
return return_str
Try posting your updated code again to improve readability. When you enter a code block into a forum post, please precede it with three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add the backticks.
This is hopefully more readable view function:
def view_settings(test_settings):
if test_settings == {}:
return("No settings available.")
else:
return_str = ("Current User Settings: \\n")
for item in test_settings.items():
for i in \[0,1\]: # list \[0,1\]
if i == 0:
return_str = return_str + item\[i\].capitalize() + ': '
if i == 1:
return_str = return_str + item\[i\] + '\\n'
return return_str
def view_settings(test_settings):
if test_settings == {}:
return("No settings available.")
else:
return_str = ("Current User Settings: \n")
for item in test_settings.items():
for i in [0,1]: # list [0,1]
if i == 0:
return_str = return_str + item[i].capitalize() + ': '
if i == 1:
return_str = return_str + item[i] + '\n'
return return_str
print(view_settings({'theme': 'dark', 'notifications': 'enabled', 'volume': 'high'}))
I copied this out of another IDE, not seeing the extra escapes, and it looks to me like the function call is together with the function
Here’s my output with the additional 2 lines at the bottom…
Theme: dark
Notifications: enabled
Volume: high
'Current User Settings: \nTheme: dark\nNotifications: enabled\nVolume: high\n'
'Current User Settings:\nTheme: dark\nNotifications: enabled\nVolume: high\n'
That did it. Many of your students will give up before they find the trivial extra space. If it were me I’d give folks helpful hints or an off-ramp after 6 or so tries. Thanks for the help.