def hotelcost(nights):
cost = 140
return nights * cost
def planeridecost(city):
if city == "Charlotte":
return 183
elif city == "Tampa":
return 220
elif city == "Pittsburgh":
return 222
elif city == "Los Angeles":
return 475
def rentalcarcost(days):
cost = 40 * days
if days >= 7:
cost - 50
elif days >= 3:
cost - 20
else:
return cost
def tripcost(city,days,spendingmoney):
return planeridecost(city) + hotelcost(days) + rentalcarcost(days) + spendingmoney
print tripcost
return tripcost
tripcost("Los Angeles",5,600)
我无法用Python解决这个问题。我已经尝试了其他几个代码来执行相同的操作,但我无法执行:
Traceback (most recent call last):
File "python", line 28, in <module>:
File "python", line 25, in tripcost:
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
答案 0 :(得分:1)
您的rentalcarcost()
函数仅返回其中一个分支中的cost
值:
def rentalcarcost(days):
cost = 40 * days
if days >= 7:
cost - 50
elif days >= 3:
cost - 20
else:
return cost
因此,只有当days
超过3时才会返回成本。其他分支也需要包含return
语句,否则您最终会返回None
:
def rentalcarcost(days):
cost = 40 * days
if days >= 7:
return cost - 50
elif days >= 3:
return cost - 20
else:
return cost
答案 1 :(得分:0)
对于天> gt = = 7且天&gt; = 3的情况,Rentalcarcost函数返回None。如下所示:
def rentalcarcost(days):
cost = 40 * days
if days >= 7:
return (cost - 50)
elif days >= 3:
return (cost - 20)
else:
return cost
你的tripcost功能有2个返回,第二个没用。