计算Python中的总成本

时间:2012-09-04 14:24:20

标签: python-2.7

我正在尝试在python中创建一个旅行计划器,但在我定义了所有我无法调用的函数并在最后一个函数tripCost()中计算它们之后。

在tripCost中,我想把日期和旅行目的地(城市)和程序运行功能,并给我以前定义的所有3个功能的确切结果。

代码:

def hotelCost(): 
    days = raw_input ("How many nights will you stay at the hotel?")
    total = 140 * int(days) print "The total cost is",total,"dollars"


def planeRideCost(): 
    city = raw_input ("Wich city will you travel to\n")
    if city == 'Charlotte':
        return "The cost is 183$"
    elif city == 'Tampa':
        return "The cost is 220$"
    elif city == 'Pittsburgh':
        return "The cost is 222$"
    elif city == 'Los Angeles':
        return "The cost is 475$"
    else:
        return "That's not a valid destination"

def rentalCarCost(): 
    rental_days = raw_input ("How many days will you rent the car\n")   
    discount_3 = 40 * int(rental_days) * 0.2 
    discount_7 = 40 * int(rental_days) * 0.5 
    total_rent3 = 40 * int(rental_days) - discount_3
    total_rent7 = 40 * int(rental_days) - discount_7
    cost_day = 40 * int(rental_days) 

    if int(rental_days) >= 3: 
        print "The total cost is", total_rent3, "dollars"
    elif int(rental_days) >= 7: 
        print "The total cost is", total_rent7, "dollars"
    else: 
        print "The total cost is", cost_day, "dollars"

def tripCost():
    travel_city = raw_input ("What's our destination\n")
    days_travel = raw_input ("\nHow many days will you stay\n")
    total_trip_cost = hotelCost(int(day_travel)) + planeRideCost (str(travel_city)) + rentalCost (int(days_travel))
    return "The total cost with the trip is", total_trip_cost

tripCost()

2 个答案:

答案 0 :(得分:0)

  1. 你的def rentalCarCost():什么也没有回复..
  2. rentalCost (int(days_travel))我看不到这样的功能。
  3. 更新

    def hotelCost(): 
        days = raw_input ("How many nights will you stay at the hotel?")
        total = 140 * int(days) print "The total cost is",total,"dollars"
        return total
    
    
    def planeRideCost(): 
        city = raw_input ("Wich city will you travel to\n")
        if city == 'Charlotte':
            return 183
        elif city == 'Tampa':
            return 220
        elif city == 'Pittsburgh':
            return 222
        elif city == 'Los Angeles':
            return 475
        else:
            print "That's not a valid destination"
    
    def rentalCarCost(): 
        rental_days = raw_input ("How many days will you rent the car\n")   
        discount_3 = 40 * int(rental_days) * 0.2 
        discount_7 = 40 * int(rental_days) * 0.5 
        total_rent3 = 40 * int(rental_days) - discount_3
        total_rent7 = 40 * int(rental_days) - discount_7
        cost_day = 40 * int(rental_days) 
    
        if int(rental_days) >= 3: 
            print "The total cost is", total_rent3, "dollars"
            return total_rent3
        elif int(rental_days) >= 7: 
            print "The total cost is", total_rent7, "dollars"
            return total_rent7
        else: 
            print "The total cost is", cost_day, "dollars"
            return cost_day
    
    def tripCost():
        travel_city = raw_input ("What's our destination\n")
        days_travel = raw_input ("\nHow many days will you stay\n")
        total_trip_cost = hotelCost(int(day_travel)) + planeRideCost () + rentalCarCost(int(days_travel))
        return "The total cost with the trip is", total_trip_cost
    
    tripCost()
    

答案 1 :(得分:0)

我建议您将用户交互与计算分开。我进行了一些更改,并以示例为例进行粘贴,并不是说此代码可以解决您的问题,而仅仅是为了说明这种分离。您还可以将cost_table用作字典:

cost_table = {
    'Charlotte':183,
    'Tampa':220,
    'Pittsburgh':222,
    'Los Angeles':475
    }

def hotel_cost(nights): 
    cost = 140 * int(nights)
    return cost

def plane_ride_cost(city):
    return cost_table[city]

def rental_car_cost(days): 
    discount_3 = 40 * days * 0.2 
    discount_7 = 40 * days * 0.5 
    total_rent3 = 40 * days - discount_3
    total_rent7 = 40 * days - discount_7
    cost_day = 40 * days 

    if days >= 3:
        return total_rent3
    elif days >= 7:
        return total_rent7
    else: 
        return cost_day

def trip_cost(city, nights, car_days):
    total = hotel_cost(nights) + plane_ride_cost(city) +\
            rental_car_cost(car_days)
    return total

city = None
while True:
    city = raw_input ("What's our destination?\n")
    if city not in cost_table:
        print "That's not a valid destination."
    else:
        break

hotel_nights = raw_input ("\nHow many nights will you stay?\n")
car_days = raw_input ("How many days will you rent the car?\n")   

total_trip_cost = hotel_cost(int(hotel_nights)) +\
                  plane_ride_cost(city) +\
                  rental_car_cost(int(car_days))

print "The total cost with the trip is", total_trip_cost, "dollars."