2016-04-01 4 views
0
def hotel_cost(): 
    nights = input("how many days?") 
    return 140 * nights 

def plane_ride_cost(): 
    city = input("which city? Charlotte, Tampa, Pittsburgh or Los Angeles?") 
    if city == "Charlotte": 
     return 183 
    elif city == "Tampa": 
     return 220 
    elif city == "Pittsburgh": 
     return 222 
    elif city == "Los Angeles": 
     return 475 


def rental_car_cost(): 
    days = input("how many days for renting a car?") 
    pro_day = days * 40 
    if days >= 7: 
     return pro_day - 50 
    elif days >=3: 
     return pro_day - 20 
    else: 
     return pro_day 

def trip_cost(): 
    return nights + city + pro_day + spending_money 

print trip_cost(hotel_cost(),plane_ride_cost(),rental_car_cost()+ spending_money) 

こんにちは、このコードを手伝ってもらえますか?私はcodeacademyでそれを学び、変更した、それはユーザーフレンドリーにしたいが、コードを実行した後、私は都市名とエラーの後の日を選択することができます。私はPythonで非常にnoobのです、アドバイスのいずれかの種類に感謝、感謝pythonシンプルバケーションスクリプト

+0

まず、関数本体の一部であるコードをインデントします。 –

+1

私の最初のアドバイスは、エラーを読んで、あなたに何を伝えているのか推測することです。私の2番目のアドバイスは、トレースバック全体のエラーを質問に入れることです。エラーがなければ、間違っていることを伝えるのは難しいです。 – zondo

答えて

1

チェック、あなたはあなたがintとstrのオブジェクトを比較するit.Alsoであなたのロジックをして、協力することができますrent_car_cost.Makeを最初にキャストするか、文字列型オブジェクトと比較してください。

def hotel_cost(): 
    nights = int(raw_input("how many days?")) 
    return 140 * nights 

def plane_ride_cost(): 
    city = raw_input("which city? Charlotte, Tampa, Pittsburgh or Los Angeles?") 
    if city == "Charlotte": 
     return 183 
    elif city == "Tampa": 
     return 220 
    elif city == "Pittsburgh": 
     return 222 
    elif city == "Los Angeles": 
     return 475 


def rental_car_cost(): 
    days = int(raw_input("how many days for renting a car?")) 
    pro_day = days * 40 
    if days >= 7: 
     return pro_day - 50 
    elif days >=3: 
     return pro_day - 20 
    else: 
     return pro_day 

def spending_money(): 
    money_spent = int(raw_input("how much money will you spend there?")) 
    return money_spent 


def trip_cost(): 
    return hotel_cost() + plane_ride_cost() + rental_car_cost() + spending_money() 


print trip_cost() 
+0

raw_inputといっしょに使ってくれてありがとう、魔法のように働いてくれてありがとう:) – elchinsolo

2

使用

raw_input("which city? Charlotte, Tampa, Pittsburgh or Los Angeles? ")

代わりの

input("which city? Charlotte, Tampa, Pittsburgh or Los Angeles? ")。このout.Youが小遣いvariable.Iは、この機能を作る欠落していた

チェックこのリンク NameError

+0

ありがとうSiddhant、very usefull)) – elchinsolo

0

まず、コードを正しくインデントする必要があります。

第二に、raw_input()input()を変更します。

def plane_ride_cost(): 
    city = raw_input("which city? Charlotte, Tampa, Pittsburgh or Los Angeles?") 

そして

def rental_car_cost(): 
    days = raw_input("how many days for renting a car?") 

第三に、あなたはここにint型に文字列の入力をキャストに入力する必要があります。

def rental_car_cost(): 
    days = raw_input("how many days for renting a car?") 
    pro_day = int(days) * 40 

あなたtrip_cost()関数は何もしていません。 nightscitypro_day、またはspending_moneyの変数はありません。関数内にprint()を移動します。

def trip_cost(): 
    return nights + city + pro_day + spending_money 

これを変更する方法はいくつかあります。あなたが関数内で、ファイルの末尾にprint()のthatsを移動し、現在はそこだreturnを削除することができ、あなたはprint()を削除し、

return hotel_cost(), plane_ride_cost(), rental_car_cost() + spending_money 

への帰国を変更して、あなたの終わりにprint(trip_cost())を追加することができますファイル。選択はあなた次第です。

+0

Aaronに感謝しています。私はすでにそれを動作させていましたが、助けてくれることを感謝しています。ヘルプの) – elchinsolo