跟老师的代码一摸一样,为什么报错,那位大佬帮个忙

我是小白,最近在学类,为什么一有类变量就报错呢,向大佬请教!
代码如下
class Car:
def int(self, model, year):
self.model = model
self.year = year

def drive(self):
    print("i can drive!")

my_car = Car(‘bmw’, ‘2022’)
print(my_car.model, my_car.year)
my_car.drive()

报错提示:Traceback (most recent call last):
File “C:\Users\Administrator\PycharmProjects\pythonProject\uygtftu.py”, line 11, in
print(my_car.model, my_car.year)
^^^^^^^^^^^^
AttributeError: ‘Car’ object has no attribute ‘model’

Jason990420
最佳答案

Refer following code

class Car:

    def __init__(self, model, year):    # Wrong statement replaced here
        self.model = model
        self.year = year

    def drive(self):
        print("i can drive!")

my_car = Car('bmw', '2022')
print(my_car.model, my_car.year)
my_car.drive()
1年前 评论
jianggaojun (楼主) 1年前
讨论数量: 2
Jason990420

Refer following code

class Car:

    def __init__(self, model, year):    # Wrong statement replaced here
        self.model = model
        self.year = year

    def drive(self):
        print("i can drive!")

my_car = Car('bmw', '2022')
print(my_car.model, my_car.year)
my_car.drive()
1年前 评论
jianggaojun (楼主) 1年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!