Trying to add a vin function to a automobile inventory program

45 views Asked by At

The legitimacy of the vin is irrelevant. I just want to make sure that the user only enters 17 digits. This is what I have so far:

#Automobile Inventory

print('Automobile Inventory')

class Automobile:

def __init__(self):
    self._make = ""
    self._model = ""
    self._year = 0
    self._color = ""
    self._mileage = 0
    self._vin = ""
def addVehicle(self):
    try:
        self._make = input('Enter vehicle make: ')
        self._model = input('Enter vehicle model: ')
        self._year = int(input('Enter vehicle year: '))
        self._color = input('Enter vehicle color: ')
        self._mileage = int(input('Enter vehicle mileage: '))
        self._vin = input('Enter vehicle vin number: ')
        if len(self._vin) == 17:
                    print("ERROR: Vin must be 17 digits or less.")
        return True
    except ValueError:
        print("ERROR: Year and mileage must be numerical")
        return False
def __str__(self):
    return '\t'.join(str(x) for x in [self._make, self._model, self._year, self._color, self._mileage, self._vin])
1

There are 1 answers

0
Alexander On

Try this:

I made some inline comments where I made changes.

def addVehicle(self):
    self._make = input('Enter vehicle make: ')
    self._model = input('Enter vehicle model: ')
    self._year = int(input('Enter vehicle year: '))
    self._color = input('Enter vehicle color: ')
    self._mileage = int(input('Enter vehicle mileage: '))
    self._vin = input('Enter vehicle vin number: ')
    if len(self._vin) != 17:  # changed to != 
        print("ERROR: Vin must be 17 digits or less.")
        return False
    # removed the try and except blocks and added the conditional below
    if not self._mileage.isnumeric() or not self._year.isnumeric(): 
        print("ERROR: Year and mileage must be numerical")
        return False
    return True