Python 我还尝试注释掉显示错误的行,并且在代码之后的每一行都显示错误
[我的代码显示打印语句的错误][1] 我的代码的Img URL [1] : 我的代码如下Python 我还尝试注释掉显示错误的行,并且在代码之后的每一行都显示错误,python,syntax-error,Python,Syntax Error,[我的代码显示打印语句的错误][1] 我的代码的Img URL [1] : 我的代码如下 #OOPR-Assgn-5 #Start writing your code here class Vehicle: def __init__(self,vehicle_id,type,cost): self.__vehicle_id=vehicle_id self.__type=type self.__cost=cost self._
#OOPR-Assgn-5
#Start writing your code here
class Vehicle:
def __init__(self,vehicle_id,type,cost):
self.__vehicle_id=vehicle_id
self.__type=type
self.__cost=cost
self.__premium_amount=None
self.__premium_percentage=None
def get_type(self):
return self.__type
def get_cost(self):
return self.__cost
def get_vehcle_id(self):
return self.__vehicle_id
def set_premium_percentage(self,type):
if get_type()=="Two Wheeler":
self.__premium_percentage=2
elif get_type()=="Four Wheeler":
self.__premium_percentage=6
else:
return None
def calculate_premium(self,cost,premium_percentage):
self.__premium_amount=self.get_cost()*(self.get_premium_amount()/100)
return self.__premium_amount
def calculate_vehicle_cost(self,cost,premium_amount):
vehicle_cost=self.get_cost()+self.calculate_premium(self.get_cost(),self.set_premium_percentage(type))
return vehicle_cost
def display_vehicle_details(self,vehicle_id,type,cost,vehicle_cost):
print("Vehicle id=",str(self.get_vehcle_id())
print("Type=",str(self.get_type()))
print("Cost without Premium=",str(self.get_cost())
print("Final Cost=",str(self.calculate_vehicle_cost())
v1=Vehicle(001,"Two Wheeler",117000)
v2=Vehicle(002,"Four Wheeler",625000)
v1.display_vehicle_details
你有未闭合的括号,这就是问题所在。从第37行开始。您必须调整方法以关闭括号:
def display_vehicle_details(self,vehicle_id,type,cost,vehicle_cost):
print("Vehicle id=",str(self.get_vehcle_id()))
print("Type=",str(self.get_type()))
print("Cost without Premium=",str(self.get_cost()))
print("Final Cost=",str(self.calculate_vehicle_cost()))
然后您将收到“001”的错误“SyntaxError:invalid token”。您可以通过将001定义为字符串来解决此问题:
v1=Vehicle('001',"Two Wheeler",117000)
v2=Vehicle('002',"Four Wheeler",625000)
最后的代码如下所示:
class Vehicle:
def __init__(self,vehicle_id,type,cost):
self.__vehicle_id=vehicle_id
self.__type=type
self.__cost=cost
self.__premium_amount=None
self.__premium_percentage=None
def get_type(self):
return self.__type
def get_cost(self):
return self.__cost
def get_vehcle_id(self):
return self.__vehicle_id
def set_premium_percentage(self,type):
if get_type()=="Two Wheeler":
self.__premium_percentage=2
elif get_type()=="Four Wheeler":
self.__premium_percentage=6
else:
return None
def calculate_premium(self,cost,premium_percentage):
self.__premium_amount=self.get_cost()*(self.get_premium_amount()/100)
return self.__premium_amount
def calculate_vehicle_cost(self,cost,premium_amount):
vehicle_cost=self.get_cost()+self.calculate_premium(self.get_cost(),self.set_premium_percentage(type))
return vehicle_cost
def display_vehicle_details(self,vehicle_id,type,cost,vehicle_cost):
print("Vehicle id=",str(self.get_vehcle_id()))
print("Type=",str(self.get_type()))
print("Cost without Premium=",str(self.get_cost()))
print("Final Cost=",str(self.calculate_vehicle_cost()))
v1=Vehicle('001',"Two Wheeler",117000)
v2=Vehicle('002',"Four Wheeler",625000)
v1.display_vehicle_details