[英]TypeError: 'int' object is not callable Python-3
I am getting TypeError: 'int' object is not callable with (my_car.time())
我收到TypeError: 'int' object is not callable with (my_car.time())
After run my class, I put the input "O" but it can not read the time().运行我的课程后,我输入了“O”,但它无法读取时间()。
The program:该程序:
class Car:
def __init__(self,speed = 0):
self.speed = speed
self.odometer = 0
self.time = 0
def say_state(self):
print("I'm going {} kph!".format(self.speed))
def accelerate(self):
self.speed += 5
def brake(self):
self.speed -= 5
def step(self):
self.odometer += self.speed
self.time += 1
def average_speed(self):
if self.time != 0:
return self.odometer / self.time
else:
pass
if __name__ == '__main__':
my_car = Car()
print("I'm a car!")
while True:
action = input("What should I do? [A]ccelerate, [B]rake, "
"show [O]dometer, or show average [S]peed?").upper()
if action not in "ABOS" or len(action) != 1:
print("I don't know how to do that")
continue
if action == 'A':
my_car.accelerate()
elif action == 'B':
my_car.brake()
elif action == 'O':
print("The car has driven {} kilometers".format(my_car.odometer))
print("The time taken is".format(my_car.time()))
elif action == 'S':
print("The car's average speed was {} kph".format(my_car.average_speed()))
my_car.step()
my_car.say_state()
Output:输出:
I'm a car!
What should I do? [A]ccelerate, [B]rake, show [O]dometer, or show average [S]peed?a
I'm going 5 kph!
What should I do? [A]ccelerate, [B]rake, show [O]dometer, or show average [S]peed?a
I'm going 10 kph!
What should I do? [A]ccelerate, [B]rake, show [O]dometer, or show average [S]peed?o
The car has driven 15 kilometers
Traceback (most recent call last):
File "D:/PyCharm/New Folder/venv/Car.py", line 42, in <module>
print("The time taken is".format(my_car.time()))
TypeError: 'int' object is not callable
Process finished with exit code 1
time
in your program is a variable, not method
of a class
so you can't call it, change line 42 to程序中的time
是一个变量,而不是class
method
,因此您无法调用它,请将第 42 行更改为
print("The time taken is {} h".format(my_car.time))
, it should fix the error. print("The time taken is {} h".format(my_car.time))
,它应该修复错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.