Python 3:使用类的车辆库存

时间:2018-06-06 13:51:37

标签: python python-3.x

我无法让我的Python程序为我的课程作业工作。我写了我认为正确的代码,但我仍然得到错误:

*NameError: name 'self' is not defined* Here is the Assignment:

创建符合下述要求的最终计划。

创建一个汽车类,将由经销商用作车辆库存计划。汽车类中应包含以下属性:

  • private string make
  • 私人字符串模型
  • 私人字符串颜色
  • private int year
  • private int mileage

您的计划应采用适当的方法,例如:

  • 构造
  • 添加新车
  • 移除车辆
  • 更新车辆属性

在程序结束时,它应该允许用户将所有车辆库存输出到文本文件。

以下是我的代码,感谢任何帮助:

    class Automobile:

    def __init__(self, make, model, color, year, mileage):
        self.make = make
        self.model = model
        self.color = color
        self.year = year
        self.mileage = mileage

    def add_vehicle(self):
        auto = Automobile()
        vehicle_file = open('vehicle.txt', 'a')
        make = input("Enter make: ")
        model = input("Enter model: ")
        color = input("Enter color: ")
        year = input("Enter year: ")
        mileage = input("Enter mileage: ")

        vehicles = Automobile(make, model, color, year, mileage)
        vehicle_list = [vehicles.make, vehicles.model, vehicles.color, vehicles.year, vehicles.mileage]

        for i in vehicle_list:
            vehicle_file.write("%s\t" % item)
            vehicle_file.write("\n")
            vehicle_file.close()
        print("Your record has been succesfully added to the inventory")

    def delete_vehicle(self):
        del_rec = input("Enter record to delete: ")

        with open("vehicle.txt","r+") as f:
            new_f = f.readlines()
            f.seek(0)
            for line in new_f:
                if del_rec not in line:
                    f.write(line)
            f.truncate()
        print("Succesfully deleted record from inventory")

    def set_make(self, make):
        self.make = make

    def get_make(self):
        return self.make

    def set_model(self, model):
        self.model = model

    def get_model(self):
        return self.model

    def set_color(self, color):
        self.color = color

    def get_color(self):
        return self.color

    def set_year(self, year):
        self.year = year

    def get_year(self):
        return self.year

    def set_mileage(self, mileage):
        self.mileage = mileage

    def get_mileage(self):
        return self.mileage

def main():
    menu = {}
    menu['1']="Add Vehicle." 
    menu['2']="Delete Vehicle."
    menu['3']="Find Vehicle"
    menu['4']="Exit"

user=True
while user:
    print ("""
    1.Add a Vehicle
    2.Delete a Vehicle
    3.View Inventory
    4.Exit/Quit
    """)
    ans=input("What would you like to do? ") 
    if ans=="1": 
      Automobile.add_vehicle
    elif ans=="2":
      Automobile.delete_vehicle(self)
    elif ans=="3":
      print(Automobile.vehicles) 
    elif ans=="4":
      print("\n Goodbye")
      break
    elif ans !="":
      print("\n Invaild Entry")

if __name__ == "__main__":
    main()

1 个答案:

答案 0 :(得分:-1)

假设您要导入此模块,您已在main中创建并使用它,它对我来说很好。您需要更改的唯一方法是将i更改为for循环中的项目,并在底部一直删除删除车辆中的self。这从我的班级中删除了所有错误。另请注意,如果你应该使用私有和受保护的变量,你需要添加ex:__ self.Make。您的变量目前不是私有或受保护的。通过添加_或__修饰符,您可以将变量设为私有或受保护。我猜你对私有和受保护的内容有一个模糊的概念,但这就是你需要getter和setter的原因。试一试,看看它是否有帮助!如果您仍然不清楚,请务必回顾私人和受保护的变量。也不确定你的添加车辆方法是否会在底部工作,因为你没有关闭括号。它应该是addvehicle()