0

I am very new to python and I am trying to write a simple vacation program. In my program, the user is required to input city, number of days of vacation etc... and in the end I calculate the total cost, based on some simple calculations defined in few functions.

My problem is, I am not able to print the output of a particular function which has two input parameters, without adding the input parameters in the print statement !!!

But, since the user enters the values, I don't want to hardcode them.

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

I am new and lost !!!

Kindly help me....

5
  • def trip_cost(city, days): city = hotel_cost(days) + plane_ride_cost(city) days = rental_car_cost(days) total_cost = city + days return total_cost Commented Jan 30, 2015 at 4:41
  • How do I print total_cost in a statement like "( "Your trip cost for %s many days of stay in %s, when you rent a car for %s number of days is %? " % (days,city,car,?) So - what EXACTLY should replace '?' here, so that the total cost is printed in the output without passing city, days in print ? Commented Jan 30, 2015 at 4:42
  • python 2 or 3? print("Your trip cost for %s many days of stay in %s, when you rent a car for %s number of days is %s " % (days,city,car,cost)) look at the string formatters docs.python.org/2/tutorial/inputoutput.html and docs.python.org/2/library/stdtypes.html#string-formatting Commented Jan 30, 2015 at 4:50
  • Python 2 - may be that's why its not working ?? Commented Jan 30, 2015 at 5:12
  • That will work for python 2, depends on how you declare the variables and where in your program. Commented Jan 30, 2015 at 5:15

4 Answers 4

1

Your function returns a value that can be passed as an argument to print() or a formatted string. For example:

print("Your %s day trip in %s is %0.2f dollars"%(days, city, trip_cost(city, days)))

EDIT: Here's a complete example, which I adapted from your comment.

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 
    else:
        raise ValueError("No Vacation")

def hotel_cost(days): 
    return 140*days

def rental_car_cost(days):
    return 30*days

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

city=raw_input("Enter your destination")
days=int(raw_input("Enter the duration of your stay in number of days"))
print("Your %s day trip in %s is %0.2f dollars"%(days, city, trip_cost(city, days)))
Sign up to request clarification or add additional context in comments.

3 Comments

city=raw_input("Enter your destination") 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 else: return "No Vacation" days=raw_input("Enter the duration of your stay in number of days") def hotel_cost(days): return 140*days . . . . .. . def trip_cost(city, days): city = hotel_cost(days) + plane_ride_cost(city) days = rental_car_cost(days) total_cost = city + days return total_cost
How do I print the "total_cost" in my print statement.
look at my comment and change cost to total_cost
0

I'm not sure I understood your question, but you can print the specific values like so:

print("City: %s\nDays: %s\nSpending money: $%s\n") % (city,days,spending_money)

Assuming input was: NYC, 10, and 12345 respectively the output would be:

City: NYC

Days: 10

Spending money: $12345

3 Comments

this notation isn't compatible with python 3
city=raw_input("Enter your destination") 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 else: return "No Vacation" days=raw_input("Enter the duration of your stay in number of days") def hotel_cost(days): return 140*days
I wish I could paste my code in some clear format - I know, it's not very clear from my question.
0

Please clarify exactly what you want to print and what version of python you are using.

def trip_cost(city, days, spending_money):
    total_cost = hotel_cost(days) + plane_cost(city) + rental_car_cost(days) + spending_money 
    print "Total Cost: " + str(total_cost)
    return total_cost

1 Comment

I am using the old version - 2.7.3
0

I'm assuming you want to calculate the total cost. In Python 3 then:

hotel_cost_amt = 200
rental_car_amt = 30
plane_ride_amt = {'city_A': 1200, 'city_B': 1100}

def hotel_cost(days):
    return days * hotel_cost_amt

def plane_ride_cost(city):
    return plane_ride_amt[city]

def rental_car_cost(days):
    return rental_car_amt * days

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

if __name__ == '__main__':
    days = 7
    city = 'city_A'
    spending_money = 2000

    print('Your trip cost for %i many days of stay in %s, when you rent a car for %i number of days is %i. This includes %i spending money.' % \
        (
            days,
            city,
            days,
            trip_cost(city, days, spending_money),
            spending_money
        )
    )

The function the way you defined it wouldn't work. For Python 2 use

print 'Your trip cost for %i many days of stay in %s, when you rent a car for %i number of days is %i. This includes %i spending money.' % \
        (
            days,
            city,
            days,
            trip_cost(city, days, spending_money),
            spending_money
        )

4 Comments

How do I combine raw_input here ? Like, when I am asking the user to input numbers and names. I tried modifying my code as per your suggestion, it is compiling fine, but I am not getting any output from print !!! I think, I need to try it in Python3. Thanks for your help.
What are you asking the user to input that you later raw_input into? Remember, that raw_input is a string so you need to cast it into either integer (using int(raw_input)) or float: float(raw_input).
Does this function look good to you(just by itself) or something is missing - car=raw_input("Enter the number of days when you will rent a car") def rental_car_cost(days): cost = 40*days if days >=7: cost-=50 elif days >=3: cost-=20 return cost
Nope, it should be car_days=int(raw_input("Enter the number of car rental days: ")) Then call rental_car_cost(car_days) The function itself looks ok as long as I understand it without indents.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.