【发布时间】:2021-09-25 15:02:58
【问题描述】:
我正在完成一项涵盖课程的作业。有几个要求,但我的程序以目前的方式几乎满足了所有这些要求。
该程序将提示用户将汽车或卡车输入他们的虚拟车库。他们从那里选择许多选项,具体取决于它是汽车还是卡车。
用户继续执行此操作,直到他们完成添加车辆,然后他们会收到有关他们输入的车辆及其信息的提示。
就目前而言,我可以输入无数的汽车或卡车,它会打印我正确输入的内容。它不允许我进入汽车和卡车,这是我需要它做的。
我知道这个问题可能与我的最后一个循环有关,因为它引用了 carTruck 并且它可能应该引用实例列表中元素的值。不过我不确定该怎么做。
我意识到可能有更好的方法来实现这一点,但有一些方法必须根据任务来完成。此外,这里不需要错误处理,因此不包括在内。
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: ').title()
return self.make
def getModel(self):
self.model = input('Please enter the vehicle model: ').title()
return self.model
def getColor(self):
self.color = input('Please enter the vehicle color: ')
return self.color
def getFuelType(self):
self.fuelType = input('Please enter the vehicle fuel type: ')
return self.fuelType
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
return self.options
#car child class
class Car(Vehicle):
def __init__ (self, make, model, color, fuelType,options, engineSize, numDoors):
self.engineSize = engineSize
self.numDoors = numDoors
Vehicle.__init__(self, make, model, color, fuelType,options)
def getEngineSize(self):
self.engineSize = input('Please enter your engine size in liters: ')
return self.engineSize
def getNumDoors(self):
self.numDoors = input('Please enter the number of doors: ')
return self.numDoors
#pickup child class
class Pickup(Vehicle):
def __init__ (self, make, model, color, fuelType,options, cabStyle, bedLength):
self.cabStyle = cabStyle
self.numDoors = bedLength
Vehicle.__init__(self, make, model, color, fuelType, options)
def getCabStyle(self):
self.cabStyle = input('Please enter the cab style: ')
return self.cabStyle
def getBedLength(self):
self.bedLength = input('Please enter the bed length: ')
return self.bedLength
#creates instance and loops to get info for vehicles from user
instances = []
Exit = 'n'
x = 0
while Exit == 'n':
carTruck = input('Are you entering a car or truck? ')
plateNum = input('please enter your license plate number: ')
instances.append(carTruck + plateNum)
#if statement to use correct class based on user input
if carTruck == 'car':
instances[x] = Car('','','','','','','')
instances[x].getMake()
instances[x].getModel()
instances[x].getColor()
instances[x].getFuelType()
instances[x].getEngineSize()
instances[x].getNumDoors()
instances[x].getOptions()
if not instances[x].options:
print('\nYou need to select at least one option.')
Vehicle.getOptions(instances[x])
elif carTruck == 'truck':
instances[x] = Pickup('','','','','','','')
instances[x].getMake()
instances[x].getModel()
instances[x].getColor()
instances[x].getFuelType()
instances[x].getCabStyle()
instances[x].getBedLength()
instances[x].getOptions()
if not instances[x].options:
print('\nYou need to select at least one option.')
Vehicle.getOptions(instances[x])
#allows user to stop adding vehicles
Exit = input('Are you done adding vehicles (Y/N): ').lower()
x = x + 1
#loops through instances and provides output dependent on whether it is a car or truck.
b = 0
while b < len(instances):
if carTruck == 'truck':
print(f'Your vehicle is a {instances[b].color} {instances[b].make} {instances[b].model} {instances[b].cabStyle} and a {instances[b].bedLength} ft bed that runs on {instances[b].fuelType}.')
print(f'The options are ' + ", ".join(instances[b].options) +'.\n')
elif carTruck == 'car':
print(f'Your vehicle is a {instances[b].color} {instances[b].make} {instances[b].model} {instances[b].numDoors} door with a {instances[b].engineSize} liter {instances[b].fuelType} engine.')
print(f'The options are ' + ", ".join(instances[b].options) +'.\n')
b = b + 1
Output:
Are you entering a car or truck? car
please enter your license plate number: 123456
Please enter the vehicle make: ford
Please enter the vehicle model: mustang
Please enter the vehicle color: red
Please enter the vehicle fuel type: gas
Please enter your engine size in liters: 5
Please enter the number of doors: 2
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: y
Does your vehicle have remote start: y
Does your vehicle have a back up camera: y
Are you done adding vehicles (Y/N): n
Are you entering a car or truck? truck
please enter your license plate number: 789456
Please enter the vehicle make: chevy
Please enter the vehicle model: 1500
Please enter the vehicle color: black
Please enter the vehicle fuel type: gas
Please enter the cab style: crew cab
Please enter the bed length: 6
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: y
Does your vehicle have remote start: y
Does your vehicle have a back up camera: y
Are you done adding vehicles (Y/N): y
Traceback (most recent call last):
File "c:\Users\chris\Desktop\School\Intro to Programming\python_work\classes.py", line 138, in <module>
print(f'Your vehicle is a {instances[b].color} {instances[b].make} {instances[b].model} {instances[b].cabStyle} and a {instances[b].bedLength} ft bed that runs on {instances[b].fuelType}.')
AttributeError: 'Car' object has no attribute 'cabStyle'
``
【问题讨论】: