python简单假期纸条

2024-04-20 12:54:10 发布

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

def hotel_cost():
    nights = input("how many days?")
    return 140 * nights

def plane_ride_cost():
    city = input("which city? Charlotte, Tampa, Pittsburgh or Los Angeles?")
    if city == "Charlotte":
        return 183
    elif city == "Tampa":
        return 220
    elif city == "Pittsburgh":
        return 222
    elif city == "Los Angeles":
        return 475


def rental_car_cost():
    days = input("how many days for renting a car?")
    pro_day = days * 40
    if days >= 7:
        return pro_day - 50
    elif days >=3:
        return pro_day - 20
    else:
        return pro_day

def trip_cost():
    return nights + city + pro_day + spending_money

print trip_cost(hotel_cost(),plane_ride_cost(),rental_car_cost()+  spending_money)

嗨,伙计们,有人能帮我处理这段代码吗?我在codeacademy上学习并修改了它,想让它更友好,但是在运行代码之后,我可以选择天、城市名称和错误之后。我对Python很在行,谢谢你的建议


Tags: cityinputreturndefcardayshotelmany
3条回答

看看这个出去。你呢缺少花钱变量。我为此做了一个函数,你可以在你的逻辑中配合是的。还有你把str对象和int在车里比较_成本。制造一定要先强制转换它,或者将它与字符串类型的对象进行比较。你知道吗

def hotel_cost():
    nights = int(raw_input("how many days?"))
    return 140 * nights

def plane_ride_cost():
    city = raw_input("which city? Charlotte, Tampa, Pittsburgh or Los Angeles?")
    if city == "Charlotte":
        return 183
    elif city == "Tampa":
        return 220
    elif city == "Pittsburgh":
        return 222
    elif city == "Los Angeles":
        return 475


def rental_car_cost():
    days = int(raw_input("how many days for renting a car?"))
    pro_day = days * 40
    if days >= 7:
        return pro_day - 50
    elif days >=3:
        return pro_day - 20
    else:
        return pro_day

def spending_money():
    money_spent = int(raw_input("how much money will you spend there?"))
    return money_spent


def trip_cost():
    return hotel_cost() + plane_ride_cost() + rental_car_cost() + spending_money()


print trip_cost()

使用

raw_input("which city? Charlotte, Tampa, Pittsburgh or Los Angeles? ")

而不是

input("which city? Charlotte, Tampa, Pittsburgh or Los Angeles? ")。你知道吗

检查此链接 NameError

首先,您需要正确地缩进代码。你知道吗

第二,将input()更改为raw_input()

def plane_ride_cost():
    city = raw_input("which city? Charlotte, Tampa, Pittsburgh or Los Angeles?")

以及

def rental_car_cost():
    days = raw_input("how many days for renting a car?")

第三,您需要在此处将字符串输入类型转换为int:

def rental_car_cost():
    days = raw_input("how many days for renting a car?")
    pro_day = int(days) * 40

你的trip_cost()函数什么也没做。您没有nightscitypro_dayspending_money的任何变量。在函数中移动print()。你知道吗

def trip_cost():
    return nights + city + pro_day + spending_money

有几种方法可以改变这种状况。您可以将文件末尾的print()移到函数中并删除当前所在的return,您可以删除print()并将返回改为

return hotel_cost(), plane_ride_cost(), rental_car_cost() + spending_money

然后在文件末尾添加一个print(trip_cost())。选择权在你。你知道吗

相关问题 更多 >