Zen Mournster Zen Mournster - 1 year ago 66
Python Question

Why can't I pass this int type variable as an argument in Python?

Below are three functions that calculates a users holiday cost. The user is encouraged to enter details of his holiday which are then passed off into the functions as arguments.

def hotel_cost(days):
days = 140*days
return days
"""This function returns the cost of the hotel. It takes a user inputed argument, multiples it by 140 and returns it as the total cost of the hotel"""

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
"""this function returns the cost of a plane ticket to the users selected city"""

def rental_car_cost(days):
if days >=7:
rental_car_cost -= 50
elif days >=3:
rental_car_cost -= 20
return rental_car_cost
"""this function calculates car rental cost"""

user_days=raw_input("how many days would you be staying in the hotel?") """user to enter a city from one of the above choices"""
user_city=raw_input("what city would you be visiting?") """user to enter number of days intended for holiday"""

print hotel_cost(user_days)
print plane_ride_cost(user_city)
print rental_car_cost(user_days)

I notice that when I print the functions above, only plane_ride_cost(user_city) runs correctly. The other two functions spit out gibberish. Why is this?

Answer Source

You need to convert the output of raw_input to int. This should work:

user_days=int(raw_input("how many days would you be staying in the hotel?")) """user  to enter a city from one of the above choices"""

Note that if user enters anything but a number, this will raise an error.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download