2016-04-22 10 views
0

私はすべての機能を単一の数量を返すようにしたいが、結果を印刷することは私にエラーを与えるとき:Pythonの例外TypeError:のためのサポートされていないオペランドのタイプ(S) - :「STR」と「int型

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' 

コードは

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

答えて

2

互換性のないタイプのオペランドは使用できません。例えば、私が持っている場合、次の自分のタイプが異なっている

x = 3 
fruit = "apples" 

私が行うことができない、

print(x + fruit) 

ので - 1が整数、他の文字列です。変数を次のように別の型に強制的にキャスト(キャスト)するとき:これは現在ストリングと互換性があり、3は "3"になります。ステートメントが成功しプリントアウトします:

3 apples 

注:STR(X)を行うときに、変数xは、まだ整数を残りますが、STR(x)は文字列を返します。

ので、機能

alquiler_de_auto(dias) 

にあなたは整数でdiasを乗算しているが、diasは整数ではありません - raw_input()は常に文字列を返すためdiasは文字列です。 int(dias)を実行すると、文字列を整数に変更して互換性のある型にすることができます。

+1

str(x)はオブジェクトxの文字列バージョンを返し、x自体は整数のままです。 – EngineerCamp

+0

だから... "int" 3/5でtranformandoを解決したと思います。関数 入力する番号は です。しかし、raw_inputチェーンは常に尋ねますか?次に、int、またはstrのこの変換は、引数に依存します。 –

+0

{model = raw_input( "Que modelo de auto llevara?") noches = int(raw_input( "Cuantas noches estara en el hotel?")) DIAS = INT(raw_input( "コピーCuantos DIAS tendra EL自動?")) シウダー= raw_input( "コピーA QUEシウダードviajara?") otros_gastos = INT(raw_input( "コピーGastos Generales?"))} –

関連する問題