这是我的代码。我的结果一直是名字错误。我该如何解决这个问题?
汽车类
class Car:
def __init__(self, year, make, speed):
self.__year_model = year
self.__make = make
self.__speed = 0
def set_year_model(self, year):
self.__year_model = year
def set_make(self, make):
self.__make = make
def set_speed(self, speed):
self.__speed = 0
def get_year_model(self):
return self.__year_model
def get_make(self):
return self.__make
def get_speed(self):
return self.__speed
#methods
def accelerate(self):
self.speed +=5
def brake(self):
self.speed -=5
def get_speed(self):
return self.speed
程序
import Lab9_prob2
def main():
year = input('Enter the car year: ')
make = input('Enter the car make: ')
speed = 0
#Accelerate 5 times
mycar.accelerate()
print('The current speed is: ', mycar.get_speed())
mycar.accelerate()
print('The current speed is: ', mycar.get_speed())
mycar.accelerate()
print('The current speed is: ', mycar.get_speed())
mycar.accelerate()
print('The current speed is: ', mycar.get_speed())
mycar.accelerate()
print('The current speed is: ', mycar.get_speed())
#Brake 5 times
mycar.brake()
print('The current speed after brake is: ', mycar.get_speed())
my_car.brake()
print('The current speed after brake is: ', mycar.get_speed())
my_car.brake()
print('The current speed after brake is: ', mycar.get_speed())
my_car.brake()
print('The current speed after brake is: ', mycar.get_speed())
my_car.brake()
print('The current speed after brake is: ', mycar.get_speed())
#Call the main function
main()
答案 0 :(得分:1)
您的代码有几个问题:
您尚未创建实例:mycar = Car(year, make, speed)
您已将mycar
重命名为my_car
:即my_car.brake()
class属性名为self.__speed
,不称为self.speed
。(加速,制动和get_speed方法)
class Car:
def __init__(self, year, make, speed):
self.__year_model = year
self.__make = make
self.__speed = 0
def set_year_model(self, year):
self.__year_model = year
def set_make(self, make):
self.__make = make
def set_speed(self, speed):
self.__speed = 0
def get_year_model(self):
return self.__year_model
def get_make(self):
return self.__make
def get_speed(self):
return self.__speed
#methods
def accelerate(self):
self.__speed +=5
def brake(self):
self.__speed -=5
def get_speed(self):
return self.__speed
def main():
year = input('Enter the car year: ')
make = input('Enter the car make: ')
speed = 0
mycar = Car(year, make, speed)
#Accelerate 5 times
mycar.accelerate()
print('The current speed is: ', mycar.get_speed())
mycar.accelerate()
print('The current speed is: ', mycar.get_speed())
mycar.accelerate()
print('The current speed is: ', mycar.get_speed())
mycar.accelerate()
print('The current speed is: ', mycar.get_speed())
mycar.accelerate()
print('The current speed is: ', mycar.get_speed())
#Brake 5 times
mycar.brake()
print('The current speed after brake is: ', mycar.get_speed())
mycar.brake()
print('The current speed after brake is: ', mycar.get_speed())
mycar.brake()
print('The current speed after brake is: ', mycar.get_speed())
mycar.brake()
print('The current speed after brake is: ', mycar.get_speed())
mycar.brake()
print('The current speed after brake is: ', mycar.get_speed())
#Call the main function
main()
答案 1 :(得分:0)
此代码适用于您:
def makeCarClass():
def initialize(self, model, color, currSpeed, maxSpeed, mpg, currFuel, mileage):
self["model"] = model
self["color"] = color
self["currSpeed"] = currSpeed
self["maxSpeed"] = maxSpeed
self["mpg"] = mpg
self["currFuel"] = currFuel
self["mileage"] = mileage
return self
def changeSpeed(self, amount):
newSpeed = self["currSpeed"] + amount
if newSpeed >= self["maxSpeed"]:
self["currSpeed"] = self["maxSpeed"]
elif newSpeed <=0:
self["currSpeed"] = 0
else:
self["currSpeed"] = newSpeed
def move(self, miles):
fuelNeeded = miles/self["mpg"]
currFuel = self["currFuel"]
if currFuel <= fuelNeeded:
self["currFuel"] = 0
self["mileage"] = self["mileage"] + currFuel * self["mpg"]
print("Warning: Your car has run out of fuel")
else:
self["currFuel"] = currFuel - fuelNeeded
self["mileage"] = self["mileage"] + miles
return {"initialize":initialize, "changeSpeed":changeSpeed, "move":move}
carClass = makeCarClass()
car1 = carClass["initialize"]({},"Ferrari", "yellow", 0, 200, 10, 40, 0)
car2 = carClass["initialize"]({},"Honda", "black", 0, 120, 30, 40, 0)
print(car1)
答案 2 :(得分:0)
级车:
def __init__(self,year,make):
self.__year_model=year
self.__make=make
self.__speed=0
def accelerate(self):
self.__speed +=5
def brake(self):
self.__speed -=5
def get_speed(self):
return self.__speed
进口车
def main(): my_car = car.Car(2019,“丰田”)
for i in range(5):
my_car.accelerate()
print("Speed = ",my_car.get_speed())
for i in range(5):
my_car.brake()
print("Speed = ",my_car.get_speed())
main()