Home > Software engineering >  Problem when indexing and iterating through dictionary
Problem when indexing and iterating through dictionary

Time:04-15

I am working on building a Vin decoder for a python class, it's going well however I am having an issue when iterating throught a dictionary. Below is all the code I have so far for this project:

class vinfo():

    def __init__(self, vin = input("Enter Vin (Without Dashes):")):

        """ Data """
        model_codes = {

        }

        year_codes = {
            "2021":"M","2020":"L","2019":"K",
            "2018":"J","2017":"H","2016":"G",
            "2015":"F","2014":"E","2013":"D",
            "2012":"C","2011":"B","2010":"A",
            "2009":"9","2008":"8","2007":"7",
            "2006":"6","2005":"5","2004":"4",
            "2003":"3","2002":"2","2001":"1",
            "2000":"Y","1999":"X","1998":"W",
            "1997":"V","1996":"T","1995":"S",
            "1994":"R","1993":"P","1992":"N",
            "1991":"M","1990":"L","1989":"K",
            "1988":"J","1987":"H","1986":"G",
            "1985":"F","1984":"E","1983":"D",
            "1982":"C","1981":"B","1980":"A"
        }

        country_codes = {
            "USA":"1", "USA":"4", "USA":"5",
            "USA":"7F","Mexico":"3X", "Mexico":"37",
            "Canada":"3A", "Canada":"3W",
            "Germany":"W", "United Kingdom":"SA",
            "United Kingdom":"SM", "Japan":"J",
            "Korea":"KL", "Korea":"KR"
        }

        engine_codes = {



        }

        if "-" in vin:
            vin = vin.replace("-", "")
        else:
            print("Thanks for entering the Vin without dashes as prompted.")

        if len(vin) == 17:
            self.country_filt = vin[0]
            self.engine_filt = vin[7]
            self.year_filt = vin[9]
            self.manufact_filt = vin[1:3]
            self.serial = vin[-6:]
            self.plant_filt = vin[10]
        else:
            print("You're dumb. Enter a real vin (must be 17 characters).")
        
        self.year = self.find(self.year_filt, year_codes)
        self.country = self.find(self.country_filt, country_codes)
        self.manufact = self.find(self.manufact_filt, model_codes)
        self.engine = self.find(self.engine_filt, engine_codes)

        print(f"You entered: {vin}")
        print(f"Year: {self.year}\nManufacturer: {self.manufact}\nCountry: {self.country}\nEngine: {self.engine}")

    def find(self, filt, dict_of_type_of_codes):
        try:
            key = (list(dict_of_type_of_codes.keys())[list(dict_of_type_of_codes.values()).index(filt)])
        except:
            key = "Not in dict"
        return key

#%% Testing 
# test vin: 48123658489411439
vinfo("48123658489411439")

Ideally it would return a print statement that contains the year the car was made and the country it was made in. The year works, but it cannot find the country. 4 is the country code in this example case, and it is clearly USA. Cannot find out why. Ignore the empty dictionaries, I will fill them out once I know that my process works.

Thanks in advance.

CodePudding user response:

First of all, you have duplicating keys in creating your dictionary. Dictionaries are hashmaps that are used by searching for a unique key's corresponding value.

{"USA": "1", "USA": "4", "USA": "5"} will only leave you with only one left {"USA": "5"}

Secondly, you are doing too much in searching the dictionary which defeats the purpose of using it. E.g. if you are searching for the country based on the code, your dictionary should be the other way around and from there you can simply retrieve the corresponding value (country) for each code:

class vinfo:

    def __init__(self, vin = input("Enter Vin (Without Dashes):")):

        """ Data """
        model_codes = {

        }

        year_codes = {
            "M":"2021","L":"2020","K":"2019",
            "J":"2018","H":"2017","G":"2016",
            "F":"2015","E":"2014","D":"2013",
            "C":"2012","B":"2011","A":"2010",
            "9":"2009","8":"2008","7":"2007",
            "6":"2006","5":"2005","4":"2004",
            "3":"2003","2":"2002","1":"2001",
            "Y":"2000","X":"1999","W":"1998",
            "V":"1997","T":"1996","S":"1995",
            "R":"1994","P":"1993","N":"1992",
            "M":"1991","L":"1990","K":"1989",
            "J":"1988","H":"1987","G":"1986",
            "F":"1985","E":"1984","D":"1983",
            "C":"1982","B":"1981","A":"1980"
        }

        country_codes = {
            "1": "USA",
            "4": "USA",
            "5": "USA",
            "7F": "USA",
            "3X": "Mexico",
            "37": "Mexico",
            "3A": "Canada",
            "3W": "Canada",
            "W": "Germany",
            "SA": "United Kingdom",
            "SM": "United Kingdom",
            "J": "Japan",
            "KL": "Korea",
            "KR": "Korea"
        }

        engine_codes = {



        }

        if "-" in vin:
            vin = vin.replace("-", "")
        else:
            print("Thanks for entering the Vin without dashes as prompted.")

        if len(vin) == 17:
            self.country_filt = vin[0]
            self.engine_filt = vin[7]
            self.year_filt = vin[9]
            self.manufact_filt = vin[1:3]
            self.serial = vin[-6:]
            self.plant_filt = vin[10]
        else:
            print("You're dumb. Enter a real vin (must be 17 characters).")
    
        self.year = self.find(self.year_filt, year_codes)
        self.country = self.find(self.country_filt, country_codes)
        self.manufact = self.find(self.manufact_filt, model_codes)
        self.engine = self.find(self.engine_filt, engine_codes)

        print(f"You entered: {vin}")
        print(f"Year: {self.year}\nManufacturer: {self.manufact}\nCountry: {self.country}\nEngine: {self.engine}")

    def find(self, filt, dict_of_type_of_codes):
        return dict_of_type_of_codes.get(filt, 'Not in dict')

#%% Testing 
# test vin: 48123658489411439
vinfo("48123658489411439")
  • Related