简体   繁体   中英

How to give return value of a function to print in Python

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....

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)))

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

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

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
        )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM