Using raw_input mixed with the practice(2)code


#1


Traceback (most recent call last):
File "C:\Python27\t7.py", line 23, in
print total_cost(days, city, extra)
File "C:\Python27\t7.py", 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
    else:
        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)


#2

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

#3

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


#4

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 )


#5

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


#6

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


#7

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


#8

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)


#9

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


#10

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