我正在嘗試爲類編寫代碼,並且對類有很大的麻煩。問題在於我們寫一個類的腳本,Car
,speed
方法設置爲'0',並顯示一系列十次迭代的速度(每次調用acceleration
和brake
五次)。 我得到的錯誤是:__init __()缺少3個必需的位置參數
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'
這是的輸出應該是什麼樣的例子:
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
而且這是我的代碼:
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()
我在這個特殊的問題上花了大量的時間,並嘗試過各種方法,我也研究過類似的問題,但都有些複雜,我只需要一個簡單的解決方案。任何和所有幫助表示讚賞。謝謝!
當你初始化這個類的時候,你需要輸入參數 – masteryoom