Python TypeError: unsupported operand type(s) for -: 'str' and 'int' -


i want functions return single numerical quantity when printing 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) -: 'str' , 'int' 

the code

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!"  

you cannot use operand incompatible types. example, if have following:

x = 3 fruit = "apples" 

i cannot do,

print(x + fruit) 

because types different - 1 integer, other string. when coercing (casting) variable type so:

print(str(x) + " " + fruit) 

they both string , compatible, , 3 becomes "3". statement print out:

3 apples 

note: when doing str(x), variable x still stays integer, str(x) returns string.

so, in function

alquiler_de_auto(dias) 

you multiplying dias integer, dias not integer - dias string because raw_input() returns string. int(dias) change string integer making them compatible types.


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -