为什么这个函数没有返回任何值?

时间:2017-09-27 17:48:55

标签: python algorithm

每个子列表始终按以下顺序包含三个元素:

  1. 会员类型:'Student''Faculty''Visitor'之一的字符串。
  2. 汽车类型:'Coupe''Sedan''SUV''Hybrid'之一的字符串。
  3. 持续时间:借车的小时数 根据会员类型,汽车可提供长达一定时间的免费服务:
  4. 'Student':前三个小时免费
  5. 'Faculty':前两个小时免费
  6. 'Visitor':第一个小时是免费的
  7. 免费服务结束后,会员会根据他们租用的汽车类型收取费用:

    • 轿车:每小时10美元
    • Coupe:每小时12美元
    • SUV:每小时13美元
    • 混合动力:每小时15美元

    在索引0处存储Sedans产生的总收入。在索引1存储Coupes产生的总收入。在索引2处存储SUV产生的总收入。在指数3处存储了Hybrids产生的总收入。

    def car_rental(rentals):
        car_type = [0, 0, 0, 0]
        for rental in rentals:
            if rental == 'Student' and car_type == "Sedan" and hours > 3:
                income = 10 * (hours - 3)
                car_type[0] += income
        for rental in rentals:
            if rental == 'Faculty' and car_type == "Sedan" and hours > 2:
               income = 10 * (hours - 2)
               car_type[0] += income
        for rental in rentals:
            if rental == 'Visitor' and car_type == "Sedan" and hours > 1:
                income = 10 * (hours - 1)
                car_type[0] += income
        for rental in rentals:
            if rental == 'Student' and car_type == "Coupe" and hours > 3:
               income = 12 * (hours - 3)
               car_type[1] += income
        for rental in rentals:
            if rental == 'Faculty' and car_type == "Coupe" and hours > 2:
               income = 12 * (hours - 2)
               car_type[1] += income
        for rental in rentals:
            if rental == 'Visitor' and car_type == "Coupe" and hours > 1:
               income = 12 * (hours - 1)
               car_type[1] += income
        for rental in rentals:
            if rental == 'Student' and car_type == "SUV" and hours > 3:
               income = 13 * (hours - 3)
               car_type[2] += income
        for rental in rentals:
            if rental == 'Faculty' and car_type == "SUV" and hours > 2:
               income = 13 * (hours - 2)
               car_type[2] += income
        for rental in rentals:
            if rental == 'Visitor' and car_type == "SUV" and hours > 1:
               income = 13 * (hours - 1)
               car_type[2] += income
        for rental in rentals:
            if rental == 'Student' and car_type == "Hybrid" and hours > 3:
               income = 15 * (hours - 3)
               car_type[3] += income
        for rental in rentals:
            if rental == 'Faculty' and car_type == "Hybrid" and hours > 2:
               income = 15 * (hours - 2)
               car_type[3] += income
        for rental in rentals:
            if rental == 'Visitor' and car_type == "Hybrid" and hours > 1:
               income = 15 * (hours - 1)
               car_type[3] += income
    
        return car_type
    

    经过测试:

    print("Testing car_rental() with rentals = [['Student','Coupe',4],['Faculty','Coupe',4],['Visitor','Coupe',4]]: " +
          str(car_rental([['Student', 'Coupe', 4], ['Faculty', 'Coupe', 4], ['Visitor', 'Coupe', 4]])))
    print("Testing car_rental() with rentals = [['Student','Coupe',4],['Faculty','SUV',4],['Visitor','Hybrid',4],['Visitor','Sedan',4]]: " +
          str(car_rental([['Student', 'Coupe', 4], ['Faculty', 'SUV', 4], ['Visitor', 'Hybrid', 4], ['Visitor', 'Sedan', 4]])))
    print("Testing car_rental() with rentals = [['Student','Coupe',3],['Faculty','SUV',2],['Visitor','Hybrid',1],['Visitor','Sedan',4]]: " +
          str(car_rental([['Student', 'Coupe', 3], ['Faculty', 'SUV', 2], ['Visitor', 'Hybrid', 1], ['Visitor', 'Sedan', 4]])))
    print()
    

1 个答案:

答案 0 :(得分:0)

问题在于您引用传递给函数的rentals参数的元素的方式。特别是在每个for rental in rentals:循环中,rental变量本身也是list,您需要使用[]索引表示法引用其内容。

这是您的代码的固定版本:

def car_rental(rentals):
    car_type = [0, 0, 0, 0]
    for rental in rentals:
        if rental[0] == 'Student' and rental[1] == "Sedan" and rental[2] > 3:
            income = 10 * (rental[2] - 3)
            car_type[0] += income
    for rental in rentals:
        if rental[0] == 'Faculty' and rental[1] == "Sedan" and rental[2] > 2:
           income = 10 * (rental[2] - 2)
           car_type[0] += income
    for rental in rentals:
        if rental[0] == 'Visitor' and rental[1] == "Sedan" and rental[2] > 1:
            income = 10 * (rental[2] - 1)
            car_type[0] += income
    for rental in rentals:
        if rental[0] == 'Student' and rental[1] == "Coupe" and rental[2] > 3:
           income = 12 * (rental[2] - 3)
           car_type[1] += income
    for rental in rentals:
        if rental[0] == 'Faculty' and rental[1] == "Coupe" and rental[2] > 2:
           income = 12 * (rental[2] - 2)
           car_type[1] += income
    for rental in rentals:
        if rental[0] == 'Visitor' and rental[1] == "Coupe" and rental[2] > 1:
           income = 12 * (rental[2] - 1)
           car_type[1] += income
    for rental in rentals:
        if rental[0] == 'Student' and rental[1] == "SUV" and rental[2] > 3:
           income = 13 * (rental[2] - 3)
           car_type[2] += income
    for rental in rentals:
        if rental[0] == 'Faculty' and rental[1] == "SUV" and rental[2] > 2:
           income = 13 * (rental[2] - 2)
           car_type[2] += income
    for rental in rentals:
        if rental[0] == 'Visitor' and rental[1] == "SUV" and rental[2] > 1:
           income = 13 * (rental[2] - 1)
           car_type[2] += income
    for rental in rentals:
        if rental[0] == 'Student' and rental[1] == "Hybrid" and rental[2] > 3:
           income = 15 * (rental[2] - 3)
           car_type[3] += income
    for rental in rentals:
        if rental[0] == 'Faculty' and rental[1] == "Hybrid" and rental[2] > 2:
           income = 15 * (rental[2] - 2)
           car_type[3] += income
    for rental in rentals:
        if rental[0] == 'Visitor' and rental[1] == "Hybrid" and rental[2] > 1:
           income = 15 * (rental[2] - 1)
           car_type[3] += income

    return car_type

print("Testing car_rental() with rentals = "
      "[['Student','Coupe',4], ['Faculty','Coupe',4], ['Visitor','Coupe',4]]: " +
      str(car_rental([['Student', 'Coupe', 4], ['Faculty', 'Coupe', 4], ['Visitor', 'Coupe', 4]])))

print("Testing car_rental() with rentals = "
      "[['Student','Coupe',4], ['Faculty','SUV',4], ['Visitor','Hybrid',4], ['Visitor','Sedan',4]]: " +
      str(car_rental([['Student', 'Coupe', 4], ['Faculty', 'SUV', 4], ['Visitor', 'Hybrid', 4], ['Visitor', 'Sedan', 4]])))

print("Testing car_rental() with rentals = "
      "[['Student','Coupe',3],['Faculty','SUV',2],['Visitor','Hybrid',1],['Visitor','Sedan',4]]: " +
      str(car_rental([['Student', 'Coupe', 3], ['Faculty', 'SUV', 2], ['Visitor', 'Hybrid', 1], ['Visitor', 'Sedan', 4]])))

输出:

Testing car_rental() with rentals = [['Student','Coupe',4], ['Faculty','Coupe',4], ['Visitor','Coupe',4]]: [0, 72, 0, 0]

Testing car_rental() with rentals = [['Student','Coupe',4], ['Faculty','SUV',4], ['Visitor','Hybrid',4], ['Visitor','Sedan',4]]: [30, 12, 26, 45]

Testing car_rental() with rentals = [['Student','Coupe',3],['Faculty','SUV',2],['Visitor','Hybrid',1],['Visitor','Sedan',4]]: [30, 0, 0, 0]

更好的解决方法是在每个嵌套rental循环开头为for子列表的每个元素提供可读名称。这些方面的东西:

def car_rental(rentals):
    car_type = [0, 0, 0, 0]
    for customer, style, hours in rentals:
        if customer == 'Student' and style == "Sedan" and hours > 3:
            income = 10 * (hours - 3)
            car_type[0] += income
    for customer, style, hours in rentals:
        if customer == 'Faculty' and style == "Sedan" and hours > 2:
           income = 10 * (hours - 2)
           car_type[0] += income
    etc ...

除此之外,您还可以将所有if语句放在一个大的for rental in rentals:循环中。这会更快,并且需要稍微缩短所需的代码量:

def car_rental(rentals):
    car_type = [0, 0, 0, 0]
    for customer, style, hours in rentals:
        if customer == 'Student' and style == "Sedan" and hours > 3:
            income = 10 * (hours - 3)
            car_type[0] += income

        if customer == 'Faculty' and style == "Sedan" and hours > 2:
           income = 10 * (hours - 2)
           car_type[0] += income

        etc ...