Python变量分配给对象变量名称

时间:2018-11-01 22:52:42

标签: python python-3.x class object attributes

我(认真地)做了python大约6个月了。我不确定我是否一直在找错地方,但是似乎找不到外行的答案。

我开始做一些OOP,并且正在命名对象car。现在,我想在car1的末尾增加间隔,例如。 car2car3stack_Reg = [] stack_Make = [] stack_Mileage = [] stack_DOI = [] def Data_Entry(): EntryVal = int(input("How many entries are you making:\t")) for i in range(EntryVal): Reg = input("\nwhat is your cars Reg Number:\t") stack_Reg.append(Reg) Make = input("what is the make of you car:\t") stack_Make.append(Make) Mileage = input("What is the mileage of your car:\t") stack_Mileage.append(Mileage) DOI = input("when was the your last inspection(xx/xx/xxxx):\t") stack_DOI.append(DOI) if i != EntryVal - 1: print("\n -=NEXT CAR=-") for i in range(EntryVal): car[i] = car(stack_Reg[i], stack_Make[i]) car[i].SetMileage(stack_Mileage[i]) car[i].SetDateOfInspection(stack_DOI[i]) car[i].PrintCarInformation() - 等...

File "car_class_inputs.py", line 63, in Data_Entry
car[i] = car(stack_Reg[i], stack_Make[i])
TypeError: 'type' object does not support item assignment

我知道这不是最好的代码,但出现此错误:

class car:
    def __init__(self, registration, make):
        self.registration = registration
        self.make = make
        self.mileage = 0
        self.doi = ""
        self.model = ""

    def GetModel(self, model):
        self.model = model
        return model

    def AddMileage(self, mileage):
        self.mileage += mileage
        return mileage

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

    def SetDateOfInspection(self, doi):
        self.doi = doi

    def PrintCarInformation(self):
        print("REG NO.:", self.registration)
        print("MAKE OF CAR:", self.make)
        print("MILES:", self.mileage)
        if self.doi == "":
            print("DOI: Not Available")
        else:
            print("DOI:", self.doi)

如果需要,这是我的课程:

<?php

$username = "z";
$check;

$host = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "iarenadatabase";

$conn = new mysqli($host, $dbusername, $dbpassword, $dbname);

if (mysqli_connect_error()) {
    die('Connect Error (' . mysqli_connect_errno()) . ')' . mysqli_connect_error();
} else {

    $result = mysqli_query($conn, "SELECT * FROM iarenadbtable
WHERE Username LIKE '%{$username}%'");

    if ($result->num_rows) {
        $check = true;
        echo json_encode($check);
    } else {
        $check = false;
        echo json_encode($check);
    }

    $conn->close();
}

// header('Location: index.html');
exit;
?>

0 个答案:

没有答案