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

Screen Link:
https://app.dataquest.io/m/481/guided-project%3A-building-fast-queries-on-a-csv/3/finding-a-laptop-from-the-id

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
            else:
                return None
            
laptops = Inventory('laptops.csv')
print(laptops.get_laptop_from_id('3362737'))
print(laptops.get_laptop_from_id('3362736'))

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]
None

What actually happened:

None
None

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:

print(laptops.get_laptop_from_id('6571244'))

gave:

[‘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.

4 Likes