Fundamental Practice Problem p.11

Screen Link:

My Code:

def check_unique_values(csv_filename, col_name):
    index_num = find_col_index(csv_filename, col_name)
    if index_num == -1:
        return None
    opened_file = open(csv_filename)
    read = reader(opened_file)
    csv = list(read)
    rows = csv[1:]
    checked = {}
    for row in rows:
        val = row[index_num]
        if val in checked:
            checked[val] += 1
        if val not in checked:
            checked[val] = 1
    for key in checked:
        if checked[key] > 1:
            return False
        return True
print(check_unique_values(csv_filename, col_name1))
print(check_unique_values(csv_filename, col_name2))

What I expected to happen:
I expected the correct bool outputs since I got the green to go to the next practice problem, but I got ‘True’ for all of the input samples.

No error feedback, just incorrect outputs.

After looking at the answer, the minor difference was that I assigned the column values to a variable. Did I do this correctly?

You have return True inside of your for loop. What do you think would happen in the first iteration of the loop?

The function finishes after the first iteration. Thank you!

1 Like