Python Forum
Functions - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Functions (/thread-3870.html)



Functions - Anele - Jul-03-2017

my question here

my code here
I am new python user as i have embark to do programming, i have a task to complete but i having some challenges.

I have been asked to create four functions, Hotel Cost,Plane Cost, Car Rental and Holiday Cost
#The first function is - Hotel cost and as to take number of nights as an argument.
def hotel_cost(number_of_nights):
    return(450 *(number_of_nights))
# The second function is - Plane Cost and as to take city as an argument and return cost of flight. use if/else if statement in the function to retrieve a price base on chosen city.
def  plane_cost(city):
    if city == "Joburg":
        return(1500)
    elif city == "Cape Town":
        return(1800)
    elif city == "Jozi":
        return (1200)
    else:
        return(0) 
#The third function is Car rental and as to take days as an argument.
def car_rental(days):
    return(250 *(days))
#The last function is Holiday cost and as to take three arguments, number of nights,city,days, and return total cost of your holiday.
def holiday_cost(number_of_nights,city,days):
    cost = (hotel_cost(number_of_nights) + plane_cost(city) + car_rental(days))
    return ("Total Holiday Cost:" + str(cost)) 

#and the last task to do on this project is to print the value of the holiday function to see the result, and thats where i am getting lost. i have trying this code
print holiday_cost
Please advise and assist as its also my first time to post i might made some mistake along.
Thanks


RE: Functions - ichabod801 - Jul-03-2017

Please use python tags. I added them for you this time, but please review the BBCode tutorial link in my signature.

You need to call the holiday_cost function, just like you (?) called the hotel_cost, plane_cost, and car_rental functions within the holiday_cost function. For example:

print holiday(2, 'Joburg', 3)
Would get you the cost of three days and two nights in Joburg.


RE: Functions - buran - Jul-03-2017

More or less, you have your functions right. The only problem is that you need to call holiday_cost when you print it

print holiday_cost(number_of_nights=6, city='Joburg', number of days=7)
That is assuming 6 nights/7 days and travel to Joburg

Apart from that, three more notes:
1. It's better to use string formatting, instead of string concatenation

def holiday_cost(number_of_nights,city,days):
    cost = (hotel_cost(number_of_nights) + plane_cost(city) + car_rental(days))
    return "Total Holiday Cost: {}".format(cost)
thus no need to convert cost to str
2. I understand you have explicit requirement to use if/elif in plane cost function. If that was not the case, I would suggest using dict:
def  plane_cost(city):
    plane_costs = {"Joburg":1500, "Cape Town":1800, "Jozi":1200}
    return plane_costs[city]
or if you want to return 0 for missing city name (In my opinion that is not good idea, because is misleading, one should rise and error in case city name is unknown to you)

def  plane_cost(city):
    plane_costs = {"Joburg":1500, "Cape Town":1800, "Jozi":1200
    return plane_costs.get(city, 0)
3. Based on your requirements, cost_of_holiday, should return the cost, so it should return number, not descriptive str. This way you can use the total cost anywhere else in your program

e.g.

def holiday_cost(number_of_nights,city,days):
    return (hotel_cost(number_of_nights) + plane_cost(city) + car_rental(days))

cost = holiday_cost(number_of_nights=6, city='Joburg', number of days=7)
print "Total Holiday Cost: {}".format(cost)



RE: Functions - Anele - Jul-04-2017

Thank you very much.