Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

difficulty with this exercise !

can anyone who knows python help me with this exercise ?


def hotel_cost(nights):
if hotel_cost == 140 / nights:
return 140 nights

def plane_ride_cost(city):
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):
rent = 40
days
if days >= 7:
rent -= 50
elif days >= 3:
rent -= 20
return rent

1- Below the existing code, define a function called | trip_cost | that takes two arguments, | city | and | days |

2- make your function return the sum of calling the
| rental_car_cost(days) | ,
| hotel_cost(days) | ,
and | plane_ride_cost(city) | functions.
It is very easy. Call all the existing functions and grab their value and add them up. Return the added up values.

Something like this:
Code
def trip_cost(city, days):
    return hotel_cost(days) + plane_ride_cost(city) + rental_car_cost(days)
(Sep 30, 2015 08:07 AM)LeHaine Wrote:  It is very easy. Call all the existing functions and grab their value and add them up. Return the added up values.

Something like this:
Code
def trip_cost(city, days):
    return hotel_cost(days) + plane_ride_cost(city) + rental_car_cost(days)

i have try more times this but it sad error : hotel_cost(1) returned None instead of the correct value 140
(Sep 30, 2015 09:06 AM)power2 Wrote:  i have try more times this but it sad error : hotel_cost(1) returned None instead of the correct value 140

Well looking at your given code for hotel_cost() it shows:
Code
if hotel_cost == 140 / nights:
return 140 * nights

If the hotel_cost variable doesn't satisfy that expression it will not return 140 * nights. It will return None. Are you missing code for that function?
Try this ;

PHP Code

<?php 
def hotel_cost
(nights):
    print 
hotel_cost
    
if hotel_cost == 140 / nights:
        return 
140 * nights
print hotel_cost( 14)
#<function hotel_cost at 0xb748c684>
#None 

hotel_cost is function not variable.If you change the hotel_cost so its can be work ..

PHP Code

<?php 
if 10 == 140 / nights:
      return 
140 * nights
print hotel_cost( 14)
#1960 
PHP Code

<?php 
def tripCost
():
    
total = 0
    n 
= raw_input("Nights?: #")
    
c = raw_input("City: ")
    
r = raw_input("Days?: ")

    
total += hotel_cost(n)
    
total += int(plane_ride_cost(c))
    
total += rental_car_cost(r)

    print (
total) 

That would be the main scheme behind it OP, now if you want me to post a shorter version, let me know. For this, you would have to input the values in manually.
(Sep 30, 2015 10:40 AM)theDIVISION Wrote:  
PHP Code

<?php 
def tripCost
():
    
total = 0
    n 
= raw_input("Nights?: #")
    
c = raw_input("City: ")
    
r = raw_input("Days?: ")

    
total += hotel_cost(n)
    
total += int(plane_ride_cost(c))
    
total += rental_car_cost(r)

    print (
total) 

That would be the main scheme behind it OP, now if you want me to post a shorter version, let me know. For this, you would have to input the values in manually.

it gives me an example :

def double(n):
return 2 n
def triple(p):
return 3
p

def add(a, b):
return double(a) + triple(b)


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by 1337ITWizKidJun 10, 2021 09:14 PM 2 197 Jun 10, 2021 09:14 PM
Last Post: 1337ITWizKid
[Java] by minerwOct 5, 2020 06:44 AM 2 303 Oct 5, 2020 06:44 AM
Last Post: minerw
[Java] by bohhSep 30, 2020 12:53 PM 3 283 Sep 30, 2020 12:53 PM
Last Post: G.P.D.