Using raw_input mixed with the practice(2)code


Traceback (most recent call last):
File "C:\Python27\", line 23, in
print total_cost(days, city, extra)
File "C:\Python27\", line 17, in total_cost
sum = cost_of_hotel(days*2) + distance_of_city(city) + extra
TypeError: cannot concatenate 'str' and 'int' objects

Hello again. It is the code I am wondering why it doesnt work and how I can use raw_input for it.

def cost_of_hotel(days):
    return days * 140
def distance_of_city(city):
    if city == "los angeles":
        return 120
    elif city == "gorgan":
        return 34
def cost_of_car(days):
    cost = days * 200
    if days >=7:
        return cost - 50
    elif days >=3 and days > 7:
        return cost - 20
        return cost
def total_cost(days, city, extra):
    sum = cost_of_hotel(days*2) + distance_of_city(city) + extra
    return sum
days = raw_input("how many days for hotel? ")
city = raw_input("what city? ")
extra = raw_input("how much extra?")

print total_cost(days, city, extra)


Hi @shab64nam,

In order for you to take inputs and return the total cost for your code to also pass the SCT you have to use your defined functions.

def total_cost(days, city, spending_money):
    return plane_ride_cost(city) + hotel_cost(days) + rental_car_cost(days) + spending_money #use of defined functions along with their arguments
days = int(raw_input("how many days for hotel? ")) #conversion of string inputs to integers
city = raw_input("what city? ")
extra = int(raw_input("how much extra?")) #same conversion here as well

print total_cost(days, city, extra) #function call


the functions have very different names from the exercise, is this exercise related or are you trying to build a similair program yourself?

raw_input stores the result as string, in case of days and extra, the result should be a integer, you can use int() to convert integer to string


Yes Dear Stetim94, I am doing in my self on python. I wanted to change it to see if I can mix what I learned till here or not. Unfortunatelly it did not work (yet )


i gave a hint as to do what you need to do, bandit coded it for you, what is not working? please clarify your question with code and error messages


Thank you so much dear Bandit. So you mean the "sum" has to be removed from the code yes?


Ohhh, I did not know that raw_input store data as string! It is why the error is there! I did not learn about int as function yet dear. So I tried to use just those I leaned mix in the practice


yes, it causes the error given trying to add integers and string together:

5 + "5"

causes an error. You learned str(), int() works the same way but then from string to integer where as str() works the opposite (from integer to string)


Thank you so much. Thank a lot for your great help. It worked and I learned it :rose:


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.