User created instance

Viewed 204

I am trying to complete a homework assignment about python classes that has a few requirements: Your program will prompt the user to create at least one object of each type (Car and Pickup). Using a menu system and capturing user input your program will allow the user the choice of adding a car or pickup truck and define the vehicle's attributes. The program will use user input to define the vehicle's attributes.

The options attribute in the parent class must be a python list containing a minimum of eight (8) options common to all vehicles.

The user will choose from a list of options to add to the vehicle's options list and must choose a minimum of one vehicle option per vehicle. When the user is finished adding vehicles to their virtual garage the program will output the vehicles in their garage and their attributes.

I haven't started on the child classes yet because I want to make sure the parent class works as needed before adding more.

My program kind of works but I have to create the instance manually.

My question is, how do I make a user-inputted instance so that I can make repeat entries?

class Vehicle:

    def __init__(self, make, model, color, fuelType,options):
        self.make = make
        self.model = model
        self.color = color
        self.fuelType = fuelType
        self.options = options
            
    def getMake(self):
        self.make = input('Please enter the vehicle make: ')
        

    def getModel(self):
        self.model = input('Please enter the vehicle model: ')
        

    def getColor(self):
        self.color = input('Please enter the vehicle color: ')
        

    def getFuelType(self):
        self.fuelType = input('Please enter the vehicle fuel type: ')
        

    def getOptions(self):
        optionslist = []
        print('\nEnter Y or N for the following options')
        radio = input('Does your vehicle have a radio: ').lower()
        bluetooth = input('Does your vehicle have bluetooth: ').lower()
        cruise = input('Does your vehicle have cruise control: ').lower()
        window = input('Does your vehicle have power windows: ').lower()
        lock = input('Does your vehicle have power locks: ').lower()
        mirror = input('Does your vehicle have power mirrors: ').lower()
        rstart = input('Does your vehicle have remote start: ').lower()
        bcamera = input('Does your vehicle have a back up camera: ').lower()

        if radio == 'y':
            optionslist.append('Radio')
        if bluetooth == 'y':
            optionslist.append('Bluetooth')
        if cruise == 'y':
            optionslist.append('Cruise Control')
        if window == 'y':
            optionslist.append('Power Windows')
        if lock == 'y':
            optionslist.append('Power Locks')
        if mirror == 'y':
            optionslist.append('Power Mirrors')
        if rstart == 'y':
            optionslist.append('Remote Start')
        if bcamera == 'y':
            optionslist.append('Backup Camera')

        self.options = optionslist    


#creates instance
selection1 = Vehicle('n','n','n','n','n')


Vehicle.getMake(selection1)
Vehicle.getModel(selection1)
Vehicle.getColor(selection1)
Vehicle.getFuelType(selection1)
Vehicle.getOptions(selection1)

#makes sure options list has at least one option
if not selection1.options:
    print('\nYou need to select at least one option.')
    Vehicle.getOptions(selection1)


print(f'Your vehicle is a {selection1.color} {selection1.make} {selection1.model} that runs on {selection1.fuelType}. The options are ' + ", ".join(selection1.options) +'.')


Outputs:
Please enter the vehicle make: ford
Please enter the vehicle model: f150
Please enter the vehicle color: grey
Please enter the vehicle fuel type: gas

Enter Y or N for the following options
Does your vehicle have a radio: y
Does your vehicle have bluetooth: y
Does your vehicle have cruise control: y
Does your vehicle have power windows: y
Does your vehicle have power locks: y
Does your vehicle have power mirrors: n
Does your vehicle have remote start: n
Does your vehicle have a back up camera: n
Your vehicle is a grey ford f150 that runs on gas. The options are Radio, Bluetooth, Cruise Control, Power Windows, Power Locks.
1 Answers

Your various get* methods would make a good class method.

class Vehicle:

    def __init__(self, make, model, color, fuelType,options):
        self.make = make
        self.model = model
        self.color = color
        self.fuelType = fuelType
        self.options = options

    # As already defined
    def getMake(self): ...
    def getModel(self): ...
    def getColor(self): ...
    def getFuelType(self): ...
    def getOptions(self): ...


    @classmethod
    def get_from_user(cls):
        # Initialize the "template" vehicle
        v = Vehicle("", "", "", "", [])

        # Configure the template
        v.getMake()
        v.getModel()
        v.getColor()
        v.getFuelType()
        v.getOptions()

        # Return the configured vehicle
        return v

Then you can create an instance with one method that handles all the user prompts

v1 = Vehicle.get_from_user()

or by getting the values yourself and passing them directly to Vehicle as arguments.

v2 = Vehicle("ford", "f150", "grey", "gas", ["Radio", "Bluetooth"])
Related