Reputation: 119
I want all the functions to return a single numerical quantity but when printing the result gives me error:
Traceback (most recent call last):
File "C:/Users/Servio/Desktop/TravelTrue.py", line 64, in <module>
print 'Its total investment is',costo_viaje(ciudad, model, dias, otros_gastos, noches) ,"$ , ", " Suerte!"
File "C:/Users/Servio/Desktop/TravelTrue.py", line 59, in costo_viaje
return type_auto(model) + alquiler_de_auto(dias) + costo_hotel(noches) + costo_del_vuelo(ciudad) + otros_gastos
File "C:/Users/Servio/Desktop/TravelTrue.py", line 41, in alquiler_de_auto
costo = costo - 100 TypeError: unsupported operand type(s) for -: 'str' and 'int'
The code is
def costo_hotel(noches):
return 140 * noches
def costo_del_vuelo(ciudad):
cities = {
"Cordoba": 821,
"Iguazu": 941,
"Ushuaia": 1280,
"Bariloche": 1848,
"Palermo": 1242,
"Francia": 6235,
"Yugoslavia": 2125,
"Vietnam": 2532,
"Buenos Aires": 2499,
"Montevideo": 2129,
"Mexico": 1499,
"Moscu": 3499,
"Maracaibo": 4499,
"Irak": 9998,
}
return cities[ciudad]
def type_auto(model):
costo_type = model
if model == "deportivo":
costo_type = 860
elif model == "familiar":
costo_type = 345
return costo_type
def alquiler_de_auto(dias):
costo = dias * 338
if dias >= 7:
costo = costo - 100
elif dias >= 3:
costo = costo - 50
return costo
model = raw_input("Que modelo de auto llevara?")
noches = raw_input("Cuantas noches estara en el hotel?")
dias = raw_input("Cuantos dias tendra el auto?")
ciudad = raw_input("A que ciudad viajara?")
otros_gastos = raw_input("Gastos Generales?")
def costo_viaje(ciudad, model, dias, otros_gastos, noches):
return type_auto(model) + alquiler_de_auto(dias) + costo_hotel(noches) + costo_del_vuelo(ciudad) + otros_gastos
print 'Its total investment is',costo_viaje(ciudad, model, dias, otros_gastos, noches) ,"$ , ", " Suerte!"
Upvotes: 0
Views: 3733
Reputation: 57964
You cannot use an operand with incompatible types. For example, if I have the following:
x = 3
fruit = "apples"
I cannot do,
print(x + fruit)
because their types are different - one is an integer, the other string. When coercing (casting) the variable into another type like so:
print(str(x) + " " + fruit)
They are now both string and compatible, and 3 becomes "3". The statement will successfully print out:
3 apples
Note: when doing str(x), the variable x still stays an integer, but str(x) returns a string.
So, in the function
alquiler_de_auto(dias)
You are multiplying dias
by an integer, but dias
is not an integer - dias
is a string because raw_input()
always returns a string. You could do int(dias)
to change the string to an integer thus making them compatible types.
Upvotes: 2