将类实现为简单脚本

时间:2018-12-20 22:59:48

标签: 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))

3 个答案:

答案 0 :(得分:1)

您的类基本上应该是用于生成引号的某些参数的包装。 (它可能更适合作为一个简单的函数,但我们将在此处进行介绍。)所有用户输入将保持您当前所拥有的方式,但是费率的计算将通过一种方法完成。您将使用该类,如下所示;我将实施课堂作为练习。

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可能有其他方法 可以利用这些信息。也许起始率可能是品牌和型号的函数,而不是一个参数。)

答案 1 :(得分:1)

在我看来,您所需要的只是一个函数,该函数接受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))

现在,如果您想拥有更多使用同一辆车的相同数据的功能(makemodeltypeengineSizeyearsClaimFree ,和startingRate),那么一个类可能会有用。

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()

使用类很方便。

答案 2 :(得分:1)

我了解您的问题,我会给您上课的样子

df = read.table(text = 
'ID  course  outcome
 10  MATH    fail
 10  PHYS    pass
 10  MATH    pass
 10  MATH    fail
 10  CHEM    pass
 11  MATH    pass
 11  PHYS    fail
 12  CMP     pass
 12  MATH    fail
 12  MATH    fail
 12  CHEM    pass
 12  MATH    pass
 12  MATH    fail', header = T)


df_result = read.table(text = 
  'ID  course  outcome
   10  MATH1    fail
   10  PHYS     pass
   10  MATH1    pass
   10  MATH2    fail
   10  CHEM     pass
   11  MATH1    pass
   11  PHYS     fail
   12  CMP      pass
   12  MATH1    fail
   12  MATH1    fail
   12  CHEM     pass
   12  MATH1    pass
   12  MATH2    fail', header = T)

使用许多参数时,请查看kwargs or args

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