【问题标题】:User created instance用户创建的实例
【发布时间】: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)

标签: python class


【解决方案1】:

您的各种get* 方法将成为一个很好的类方法。

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

然后您可以使用一种方法创建一个实例来处理所有用户提示

v1 = Vehicle.get_from_user()

或者通过自己获取值并将它们作为参数直接传递给Vehicle

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

【讨论】:

  • 不幸的是,我们必须使用各种 get 方法来跟踪特定的类分解。各种get方法都是必需的函数。
  • 呃。请注意,这是一个糟糕的类设计,至少在 Python 中是这样。
  • 用户如何创建实例?我觉得需要将实例存储在列表或字典中,以便可以根据需要检索它们,但不知道该怎么做。
  • 我不确定你的意思。我将向您展示如何创建实例的两个示例。 v2 不需要任何用户交互;您可以提供您喜欢的任何值作为参数。如果您想要一个包含 5 个实例的列表,您可以执行 vs = [Vehicle.get_from_user() for _ in range(5)] 之类的操作。不过,这与Vehicle 的定义无关。
  • 抱歉,我对编码很陌生。只有在用户需要时才应创建实例。因此,如果用户添加了一辆车,那么应该只创建一个实例。如果用户添加 4 辆车,则应添加 4 辆。我可能会误解,但你展示的方式,实例是由代码预先确定的。我希望这是有道理的。通过使用列表和循环,我对此有了更深入的了解。有没有办法发布我更新的程序和输出?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-19
  • 2021-02-04
  • 1970-01-01
  • 2015-10-20
  • 1970-01-01
  • 2018-08-22
  • 2017-12-30
相关资源
最近更新 更多