Guided Project: Building Fast Queries on a CSV: A vanished laptop

Screen Link:

My Code:

class Inventory:
    def __init__(self, csv_filename):
        with open(csv_filename) as f:
            read_file = list(csv.reader(f))
            self.header = read_file[0]
            self.rows = read_file[1:]
        for row in self.rows:    
            row[-1] = int(row[-1])
    def get_laptop_from_id(self, laptop_id):
        for row in self.rows:
            if row[0] == laptop_id:
                return row
                return None
laptops = Inventory('laptops.csv')

What I expected to happen:

['3362737', 'HP', '250 G6', 'Notebook', '15.6', 'Full HD 1920x1080', 'Intel Core i5 7200U 2.5GHz', '8GB', '256GB SSD', 'Intel HD Graphics 620', 'No OS', '1.86kg', 575]

What actually happened:


The suggested laptop_id ‘3362737’ does not refer to any laptop. Maybe the data has changed over time compared to the solution notebook, but for other rows it is correct.

So for any future people in this project, if you have a code similar to mine, you are doing fine. since:



[‘6571244’, ‘Apple’, ‘MacBook Pro’, ‘Ultrabook’, ‘13.3’, ‘IPS Panel Retina Display 2560x1600’, ‘Intel Core i5 2.3GHz’, ‘8GB’, ‘128GB SSD’, ‘Intel Iris Plus Graphics 640’, ‘macOS’, ‘1.37kg’, 1339]

If you still find a mistake in my code, please tell me, but I believe the assignment laptop has vanished :wink:

Of course it is wonderful reacting to oneself with the answer.

My code only returns the first instance, any other laptop would return None. That is because I use the else: clause. This executes immediately so it will only see the first laptop in the iteration. Silly me, learned a lot today.