簡體   English   中英

python簡單的假期腳本

[英]python simple Vacation script

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中非常菜鳥,感謝任何建議,謝謝

采用

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

代替

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

檢查此鏈接NameError

檢查一下。您丟失了花錢變量。為此我創建了一個函數,可以在其中配合您的邏輯。還要在str_object與int在rental_car_cost中進行比較。請務必先將其強制轉換或與之比較字符串類型的對象。

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()

首先,您需要正確縮進代碼。

其次,將input()更改為raw_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?")

第三,您需要在此處將String輸入轉換為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()移至函數中並刪除當前存在的return ,也可以刪除print()並更改為

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

然后在文件末尾添加一個print(trip_cost()) 這個選擇由你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM