无法计算tripcost,而是抱怨NoneType

2024-10-02 16:22:59 发布

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

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解决这个问题。我尝试了其他几种代码来执行相同的操作,但无法执行:

^{pr2}$

Tags: cityreturnifdefdayscosteliflos
2条回答

对于days>;=7和days>;=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函数有两个返回值,第二个返回值无效。在

您的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

^{pr2}$

相关问题 更多 >