__init__() 缺少 3 个必需的位置参数 [英] __init__() missing 3 required positional arguments

查看:163
本文介绍了__init__() 缺少 3 个必需的位置参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为类编写代码,但在类方面遇到了巨大的麻烦.问题是我们编写一个具有一个类 Car 的脚本,将 speed 方法设置为0",并在一系列十次迭代中显示速度(分别调用 accelerationbrake 5 次).我得到的错误是:

<块引用><块引用><块引用>

回溯(最近一次调用最后一次):文件C:/Users/Brown Bear/Documents/Wake Tech/CIS115/Python Documents/Lab14P1.py",第 36 行,在 <module> 中.主要的()文件C:/Users/Brown Bear/Documents/Wake Tech/CIS115/Python Documents/Lab14P1.py", line 27, in mainmy_speed = 汽车()类型错误:__init__() 缺少 3 个必需的位置参数:'make'、'model' 和 'speed'

这是输出应该是什么的例子:

输入您的汽车型号:普锐斯输入您的汽车品牌:丰田当前速度:5当前速度:10当前速度:15当前速度:20当前速度:25当前速度:20当前速度:15当前速度:10当前速度:5当前速度:0

这是我的代码:

类汽车:def __init__(self, make, model, speed):self.make = 制作self.model = 模型self.speed = 速度def加速(自我):自身速度 += 5def 刹车(自己):self.speed -= 5def get_make(self):返回 self.makedef get_model(self):返回 self.modeldef get_speed(self):返回自身速度定义主():manuf = input('输入汽车品牌:')mod = input('请输入车型:')my_speed = 汽车()对于范围内的 num(5):my_speed.accelerate()print('当前速度:', my_speed.get_speed())对于范围内的 num(5):my_speed.brake()print('当前速度:', my_speed.get_speed())主要的()

我在这个特定问题上花费了大量时间并尝试了各种方法,我也研究了之前在这里提出的类似问题,但所有问题都太复杂了,我只需要一个简单的解决方案.任何和所有的帮助表示赞赏.谢谢!

解决方案

当您创建类的实例时,您不会传递任何参数:

 my_speed = Car()

但是你的定义说有 3 个必需的参数:

def __init__(self, make, model, speed):

因此您需要创建一个 Car 实例并传递 3 个参数:make、model 和 speed.

my_speed = Car(manuf, mod, some_Speed_val)

I'm attempting to write a code for class and am having immense trouble with classes. The problem is for us to write a script with one class, Car, have the speed method set to '0', and display the speed over a series of ten iterations (calling acceleration and brake five times each). The error I'm getting is:

Traceback (most recent call last): File "C:/Users/Brown Bear/Documents/Wake Tech/CIS115/Python Documents/Lab14P1.py", line 36, in <module> main() File "C:/Users/Brown Bear/Documents/Wake Tech/CIS115/Python Documents/Lab14P1.py", line 27, in main my_speed = Car() TypeError: __init__() missing 3 required positional arguments: 'make', 'model', and 'speed'

And this is the example of what the output should be:

Enter model of your car: Prius
Enter make of your car: Toyota
Current speed: 5
Current speed: 10
Current speed: 15
Current speed: 20
Current speed: 25
Current speed: 20
Current speed: 15
Current speed: 10
Current speed: 5
Current speed: 0

And this is my code:

class Car:
    def __init__(self, make, model, speed):
        self.make = make
        self.model = model
        self.speed = speed

    def accelerate(self):
        self.speed += 5

    def brake(self):
        self.speed -= 5

    def get_make(self):
        return self.make

    def get_model(self):
        return self.model

    def get_speed(self):
        return self.speed


def main():
    manuf = input('Enter the car make: ')
    mod = input('Enter the car model: ')

    my_speed = Car()

    for num in range(5):
        my_speed.accelerate()
        print('Current speed: ', my_speed.get_speed())
    for num in range(5):
        my_speed.brake()
        print('Current speed: ', my_speed.get_speed())

main()

I've spent a ridiculous amount of time on this particular problem and tried various ways, I've also researched similar questions previously asked on here but all are a bit too complex and I just need a simple solution. Any and all help appreciated. Thanks!

解决方案

When you create an instance of your class you are not passing any arguments:

 my_speed = Car()

But your definition says there are 3 required arguments:

def __init__(self, make, model, speed):

So you need to create an instance of Car and pass 3 arguments: make, model and speed.

my_speed = Car(manuf, mod, some_Speed_val)

这篇关于__init__() 缺少 3 个必需的位置参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆