将类实现为简单的脚本

2024-07-01 07:57:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在做一个简单的脚本练习,根据用户输入计算汽车保险。我想通过在脚本中实现类来更进一步。我试着看了一些视频和指南,但我不完全理解这个概念。你知道吗

有人能给我指出正确的方向吗?你知道吗

其目的是将当前脚本转换为一个名为Quote的类,该类反过来计算汽车保险。你知道吗

print("Hello")
print("Please enter your car insurance details:")
carMake = str(input("Enter your car make: "))
carModel = str(input("Enter your car model: "))
carType = str(input("Is your car type \"Sport\" or \"Family: \""))
engineSize = float(input("Enter your engine size in litres: "))
yearsClaimFree = int(input("Enter Years you have been claim free: "))
startingRate = 300

if engineSize >= 2.0:
    startingRate = startingRate * 2.5
elif engineSize >= 1.4:
    startingRate = startingRate * 1.5
else:
    print("Engine size is not effecting car insurance quote.")

if carType == "sport":
    startingRate = startingRate + 400
else:
    print("Car type is not effecting the car insurance quote.")

if yearsClaimFree >= 10:
    startingRate = startingRate - startingRate / 5
else:
    print("No discount gained from years claimed free.")

print("Your car insurance is €" + str(startingRate))

Tags: 脚本inputyourifiscarelseprint
3条回答

您的类基本上应该是一些用于生成报价的参数的包装器。(作为一个简单的函数,它可能更合适,但我们将在这里使用它。)所有用户输入都将保持当前的方式,但速率的计算将在一个方法中完成。您将使用这个类,如下所示;我把实现这个类作为一个练习。你知道吗

print("Hello")
print("Please enter your car insurance details:")
car_make = str(input("Enter your car make: "))
car_model = str(input("Enter your car model: "))
car_type = str(input("Is your car type \"Sport\" or \"Family: \""))
engine_size = float(input("Enter your engine size in litres: "))
years_claim_free = int(input("Enter Years you have been claim free: "))

q = Quote(small_engine=1.4, medium_engine=2.0, claim_free=10, starting_rate=300)

quote = q.generate_quote(car_make, car_model, car_type, engine_size, years_claim_free)

print("Your car insurance is €{}".format(quote))

(是否定义generate_quote采用品牌和型号取决于您自己; 当前脚本中没有使用它,但是Quote可能有其他方法 会利用这些信息。或者,启动速率可能是品牌和型号的函数,而不是单个参数。)

在我看来,您所需要的只是一个函数,它接受carMakecarModelcarType等,然后包含所有这些if语句。如果您想输入有关汽车的所有数据,然后用相同的数据调用多个不同的函数,那么类将非常有用。你知道吗

下面是如何使用函数:

def calcInsurance (make, model, type, engineSize, yearsClaimFree, startingRate):
    if engineSize >= 2.0:
        startingRate = startingRate * 2.5
    elif engineSize >= 1.4:
        startingRate = startingRate * 1.5
    else:
        print("Engine size is not effecting car insurance quote.")

    if carType == "sport":
        startingRate = startingRate + 400
    else:
        print("Car type is not effecting the car insurance quote.")

    if yearsClaimFree >= 10:
        startingRate = startingRate - startingRate / 5
    else:
        print("No discount gained from years claimed free.")
    print("Your car insurance is €" + str(startingRate))

现在,如果您希望有更多的函数使用关于同一辆车的相同数据(makemodeltypeengineSizeyearsClaimFreestartingRate),那么类可能很有用。你知道吗

class Car:
    def __init__ (self, make, model, type, engineSize, yearsClaimFree, startingRate):
        self.make = make
        self.model = model
        # etc.
    def calcInsurance (self):
        # this function is exactly the same as I showed you before, except each of the car variables need to be preceded with self.

car = Car(...car_data...)
car.calcInsurance()

如果你有更多的功能,你想做的汽车,你可以调用他们与相同的数据,这辆车只是通过使用

car.calcInsurance()
car.doSomethingElse()

这就是使用类派上用场的地方。你知道吗

我理解你的问题,我会给你上课的样子

class Quote:

def __init__(self, car_make, car_model, car_type, engine_size, years_claim_free, starting_rate):
    self.car_make = car_make
    self.car_model = car_model
    .....
    #you get the idea

def engine_size_impact_on_quote(self):
    #Your Code here and return something

def carType(self):
    #Evaluate car type

def calculate_insurance(self):
    #calculate insurance
    return self.starting_rate
#Create instance of the class
johns_quote = Quote(car_make, car_model...........)
johns_insurance = johns_quote.calculate_insurance()

print(f"John's Car insurance is {johns_insurance}"

在处理许多参数时,请查看kwargs or args。你知道吗

Python类可能有点混乱,我花了一段时间才真正理解它们,有大量的Python OOP教程

相关问题 更多 >

    热门问题