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

Question:

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):
    rental_car_cost=40*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?

Asked By: Zen Mournster

||

Answers:

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.

Answered By: Carles Mitjans

You should convert user_days to int from str.

def hotel_cost(days):
    days = 140*days
    return days


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):
    rental_car_cost=40*days
    if days >=7:
        rental_car_cost -= 50
    elif days >=3:
        rental_car_cost -= 20
    return rental_car_cost

user_days=int(raw_input("how many days would you be staying in the hotel?"))
user_city=raw_input("what city would you be visiting?")


print hotel_cost(user_days)
print plane_ride_cost(user_city)
print rental_car_cost(user_days)
Answered By: Sameer Kulkarni
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.