Using a Dictionairy of Lists

Can anyone help me with this? I’m throwing all sorts of syntax errors because I don’t understand how to format a Dictionary of Lists. I feel like my while loops are also a bit messy now.


#Color Dictionairy
#David S Milgate
#4/21/2024
#Create a dictionairy of students and their favorite colors

oldName = input("enter your name: ")

nameDict = {"name":[], "color":[]};

NoColor = False

#"""Check to see if my name and color are on the list"""
if oldName not in nameDict:
    NoColor = False
if oldName in nameDict:
    NoColor = True
    print("your favorite color is: " + str(nameDict[oldName]))
    break
"""If my name is on the list, tell me what my favorite color is"""

if oldName not in nameDict:
    NoColor = False

#adding new name to Dictionairy
while NoColor = False:
    print("your name is not on our list.")
    newColor = str(input("enter your favorite color: "))
    nameDict["name"].append(oldName)
    nameDict["color"].append(newColor)
    NoColor = True
    print("your favorite color is: " + str(nameDict[oldName]))

There are only two syntax errors

while NoColor = False

It should be while NoColor is False. The = is for assignments only, not for equality tests.

And

if oldName in nameDict:
    NoColor = True
    print("your favorite color is: " + str(nameDict[oldName]))
    break

This break is invalid. You can only break inside a loop, and there is no loop here.

None of these are about dictionary. Speaking of which, your dictionary should be

student_color = {} 
# key: value is of the form name: color

You add student colors like this

student_color["David"] = "green"
student_color["Eliza"] = "yellow"

From this, can you see how to fix your code? Feel free to ask more questions :slightly_smiling_face:

#Color Dictionairy

It’s spelled “dictionary”.

#David S Milgate
#4/21/2024
#Create a dictionairy of students and their favorite colors

oldName = input("enter your name: ")

nameDict = {"name":[], "color":[]};

You don’t need the semicolon at the end.

NoColor = False

#"""Check to see if my name and color are on the list"""
if oldName not in nameDict:
    NoColor = False
if oldName in nameDict:
    NoColor = True
    print("your favorite color is: " + str(nameDict[oldName]))
    break

break is for leaving a loop, but you’re not in a loop.

"""If my name is on the list, tell me what my favorite color is"""

if oldName not in nameDict:
    NoColor = False

#adding new name to Dictionairy
while NoColor = False:

= is for assigning, == is for checking for equality.

    print("your name is not on our list.")
    newColor = str(input("enter your favorite color: "))

input returns a string, so you don’t need the str.

    nameDict["name"].append(oldName)
    nameDict["color"].append(newColor)
    NoColor = True
    print("your favorite color is: " + str(nameDict[oldName]))

Naming flags with negative words, e.g. NoColor, can be a little unclear when it comes to what their negative means.

Your code says that NoColor is false if the name isn’t in the dictionary, which seems confusing to me.

Why do you have a dictionary of lists anyway?

If you want to store each person’s favourite colour, wouldn’t it be simpler to have a dictionary where the key is the name and the value is the colour?

1 Like

is isn’t an equality test, it’s an identity test. It should be while NoColor == False: or, better still, while not NoColor: (excepting that the double negative of not NoColor is confusing).

1 Like

Thanks a bunch! That’s all the errors from my initial code. Now I just need to figure out how to have it store this data as a .txt file so that the program repeats when it doesn’t have the name on the dictionary.

Like this

import json

# example data
student_color = {"David": "green", "Eliza": "yellow"}

file_path = ... # where you want to save the data

# save to file
with open(file_path, "w") as f:
    json.dump(student_color, f)

# read from file
with open(file_path, "r") as f:
    student_color = json.load(f)

Although python doesn’t care, it’s customary to name the file something.json (not .txt) because this is the json format. But under the hood it’s just a text file.

Based on description above shouldn’t dictionary be in form of student: color (key: value) pairs without any lists? Like:

favorites = {'Bob': 'black', 'Alice': 'white'}
1 Like

Tiny thing: False (and True) are singletons. Therefore the identity (is) test works for it (and is slightly faster than ==).

using == is of course fine here for an equality check as well.