获取位置而不是数据Python类

这是针对Python类的。 这是作业。

创建一个满足以下概述要求的最终程序。 创建将由经销商用作车辆库存计划的汽车类。汽车类中应包含以下属性: 私人琴弦制作 私有字符串模型 私人字串颜色 私人诠释年 私人int里程 您的程序应具有适当的方法,例如: 建设者 添加新车 卸下车辆 更新车辆属性 在程序结束时,它应允许用户将所有车辆清单输出到文本文件。

这是我的代码。

class Inventory:  # create class for inventory
   def __init__(self):
        self.vin = 0
        self.make = " "
        self.model = " "
        self.color = " "
        self.year = 0
        self.mileage = 0

    def add_automobile(self):  # create function to call for adding to inventory
        self.vin = int(input("Enter vin number of the vehicle: "))
        self.make = input("Enter the make of the vehicle: ")
        self.model = input("Enter the model of the vehicle: ")
        self.color = input("Enter the color of the vehicle: ")
        self.year = int(input("Enter the year of the vehicle: "))
        self.mileage = int(input("Enter the mileage of the vehicle: "))
        print(self.vin," ",self.make," Automobile added")

    def update_automobile(self):  # create function to call for updating inventory
        self.vin = int(input("Enter updated vin: "))
        self.make = input("Enter updated make: ")
        self.model = input("Enter updated model: ")
        self.color = input("Enter updated color: ")
        self.year = int(input("Enter updated year: "))
        self.mileage = int(input("Enter updated mileage: "))
        print(self.vin,"Automobile updated: ")


def main():
    vehicle = Inventory()
    objects = []
    flag = 0

    while True:
        print("1. Enter one to add vehicle to inventory")
        print("2. Enter two to update vehicle in inventory")
        print("3. Enter three to remove vehicle from inventory")
        print("4. Enter four to end and write to vehicle inventory")
        print("5. Enter five to view inventory")

        user_option = int(input("\n\nEnter numeric choice 1-4: "))
        if user_option == 1:
            vehicle.add_automobile()
            objects.append(vehicle)
            print(str(vehicle))

        elif user_option == 2:
            flag = 0
            vin_choice = int(input("\nEnter vin to update: "))
            for obj in objects:
                if obj.vin == vin_choice:
                    obj.update_automobile()
                    flag = 1
                    break
                if flag == 0:
                    print(vin_choice," not found")

        elif user_option == 3:
            flag = 0
            vin_choice = int(input("\nEnter vin to remove vehicle from inventory: "))
            for obj in objects:
                if obj.vin == vin_choice:
                    objects.remove(obj)
                    del obj
                    flag = 1
                    break
                if flag == 0:
                    print(vin_choice," not found")

        elif user_option == 4:
            file = input("Enter name of file to write to: ")
            f = open(file,"w")
            f.write(str(objects))
            f.close()

        elif user_option == 5:
            def __str__(self):
                print(vehicle)
                print(objects)


        else:
            print("We are done here. Please try again")
            break


main()

我的具体问题是,我一直在获取数据在类中的位置,而不是数据。我尝试过__str函数,但是我对类或Python的理解不够,无法将其传递给代码中的其他问题。

我现在所做的更改所带来的突破远不止于解决。我必须删除私有变量,因为这会导致更多错误。这是输出。我知道那是因为一条印刷声明。但是把它带回课堂是我的超越。 enter image description here

qayin123 回答:获取位置而不是数据Python类

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3169518.html

大家都在问