【发布时间】:2021-07-16 14:54:03
【问题描述】:
我正在尝试完成一项有关 Python 类的家庭作业,该类有一些要求: 您的程序将提示用户为每种类型(汽车和皮卡)创建至少一个对象。使用菜单系统并捕获用户输入,您的程序将允许用户选择添加汽车或皮卡车并定义车辆的属性。该程序将使用用户输入来定义车辆的属性。
父类中的 options 属性必须是一个 python 列表,其中至少包含所有车辆共有的八 (8) 个选项。
用户将从选项列表中进行选择以添加到车辆的选项列表中,并且必须为每辆车选择至少一个车辆选项。当用户完成将车辆添加到他们的虚拟车库时,程序将输出他们车库中的车辆及其属性。
我还没有开始创建子类,因为我想在添加更多之前确保父类按需要工作。
我的程序可以运行,但我必须手动创建实例。
我的问题是,如何制作用户输入的实例以便重复输入?
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.
【问题讨论】:
-
将输入语句移到外面
-
无论您使用什么函数来从用户那里获取选项,都需要返回一个类实例 -
return Vehicle(make, model, color, fuelType, options)