每个子列表始终按以下顺序包含三个元素:
'Student'
,'Faculty'
或'Visitor'
之一的字符串。'Coupe'
,'Sedan'
,'SUV'
或'Hybrid'
之一的字符串。'Student'
:前三个小时免费'Faculty'
:前两个小时免费'Visitor'
:第一个小时是免费的免费服务结束后,会员会根据他们租用的汽车类型收取费用:
在索引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()
答案 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 ...