问题描述
我的代码的网址 [1]:https://i.stack.imgur.com/AymWq.png
我的代码在下面给出
#OOPR-Assgn-5
#Start writing your code here
class Vehicle:
def @R_404_3979@(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,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_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,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,premium_percentage):
self.__premium_amount=self.get_cost()*(self.get_premium_amount()/100)
return self.__premium_amount
def calculate_vehicle_cost(self,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,str(self.calculate_vehicle_cost()))
v1=Vehicle('001',625000)
v1.display_vehicle_details