为python添加多个新函数

2024-05-15 11:12:38 发布

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

因此,我在codeacademy学习python教程时遇到了这个问题
代码是这样的

def hotel_cost(days):
    return days*140

Charlotte = "Charlotte"
Tampa = "Tampa"
Pittsburgh = "Pittsburgh"
LosAngeles = "Los Angeles"


def plane_ride_cost(city):
    if city == Charlotte:
        return 183
    elif city == Tampa:
        return 220
    elif city == Pittsburgh:
        return 222
    elif city ==LosAngeles:
        return 475
    else:
        return "not"

def rental_car_cost(days):
    if days <3:
       return days*40
    if days >=3 and days<7:
       return days*40-20
    if days >=7:
        return days*40-50

def trip_cost(city,days):
    return hotel_cost(days)+rental_car_cost(days)+plane_ride_cost(city)

我再次意识到

def trip_cost(city,days):
    return hotel_cost(days)+rental_car_cost(days)+plane_ride_cost(city)

但是我没有足够的知识去修改它(我试过)

非常感谢所有看过这个的人


Tags: cityreturnifdefcardayshotelcost
2条回答

我复制粘贴了你的代码,一切正常,但没有输出。你知道吗

所以没有任何东西出现会让你觉得你的代码错了吗?你知道吗

只需在末尾加上这一行:

print trip_cost('Tampa', 10)

您将看到输出

你可以写一堆printraw_input来要求用户输入一个城市和一天。你知道吗

编辑

哦,我没有注意到错误的默认返回值plane_ride_cost(city),只要将其更改为0,或者当用户输入错误的城市时,其他一些东西是有意义的。你知道吗

以下函数中存在问题。返回not而不是整数。然后尝试在trip\u cost方法中添加返回值。这样不行。考虑返回一个0而不是not,然后事情就应该正常了

def plane_ride_cost(city):
    if city == Charlotte:
        return 183
    elif city == Tampa:
        return 220
    elif city == Pittsburgh:
        return 222
    elif city ==LosAngeles:
        return 475
    else:
        return "not"  

相关问题 更多 >