2014-09-27 58 views
2

我想學習使用codeacdemy的python。這是他們的一個練習。基本上他們讓我創造了4種不同的功能來計算總成本。但是沒有選擇要求用戶手動輸入值。所以這就是我想要的。該代碼是正確的return rental-car_cost部分。它只是我遇到麻煩的最低點。獲取用戶輸入功能

print "this code calculates the total price of a trip, using 4 functions" 

def hotel_cost(nights): 
    return 140*nights 

def plane_ride_cost(city): 
    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): 
    cost=days*40 

    if (days>=7): 
     cost -= 50 

    elif(days>=3): 
     cost -=20 

    return cost 

def trip_cost(city,days,spending_money): 
    return rental_car_cost(days)+hotel_cost(days)+ plane_ride_cost(city)+spending_money 

city= raw_input("enter city name") 
days= raw_input("enter number of days staying") 
spending_money= raw_input("enter spendig money") 
print trip_cost(city,days, spending_money) 

這是原始代碼,它符文完美。我想要做的就是讓用戶在運行代碼時輸入值。

def hotel_cost(nights): 
    return 140*nights 

def plane_ride_cost(city): 
    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): 
    cost=days*40 

    if (days>=7): 
     cost -= 50 

    elif(days>=3): 
     cost -=20 

     return cost 

def trip_cost(city,days,spending_money): 
    return rental_car_cost(days)+hotel_cost(days)+ plane_ride_cost(city)+spending_money 

print trip_cost("Los Angeles",5,600) 
+0

順便說一句我知道我只能說print trip_cost(「洛杉磯」,5,100),那會給我答案。但我想在控制檯中詢問用戶而不必每次都更改代碼。感謝您的幫助 – user4081147 2014-09-27 01:29:40

+1

您需要修復縮進。 – 2014-09-27 01:33:15

+0

我認爲我的縮進適合原始代碼。我的代碼運行無錯誤,我知道100%正確的返回rental_car_cost(天)+酒店成本(天)+ ..... – user4081147 2014-09-27 01:41:39

回答

0

嘗試使用int(raw_input(enter number of days staying"))input("enter number of days staying")而不是raw_input("enter number of days staying")會發生什麼情況?你看到有什麼不同?這是因爲raw_input()將輸入數據轉換爲字符串,但與input()不同。找出input()和raw_input()之間的差異,以及它如何隨着python的發展而發生變化。我已經對代碼進行了一些更改,如下所示。它完美無誤地運行。讓我知道它是否對你有幫助。

print "this code calculates the total price of a trip, using 4 functions" 

def hotel_cost(nights): 
    return 140*nights 

def plane_ride_cost(city): 
    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): 
    cost=days*40 

    if (days>=7): 
     cost -= 50 

    elif(days>=3): 
     cost -=20 

    return cost 

def trip_cost(city,days,spending_money): 
    return rental_car_cost(days)+hotel_cost(days)+ plane_ride_cost(city)+spending_money 

city= raw_input("enter city name") 
days= int(raw_input("enter number of days staying")) ##### notice here 
spending_money= int(raw_input("enter spendig money")) ### and here too 
print trip_cost(city,days, spending_money) 

而不是以上,你可以使用下面的代碼。

################## without type casting############# 
    city= raw_input("enter city name") 
    days= input("enter number of days staying") ##### notice something here 
    spending_money= input("enter spendig money") ### and here 
    print trip_cost(city,days, spending_money) 
+0

thx這麼多我現在明白了,代碼工作完美,運行也很好,謝謝!!!! – user4081147 2014-09-27 16:41:23

+0

不要忘了給我的答案+1,不客氣 – 2014-09-27 17:06:46

+0

我想說但是它說我需要15分 – user4081147 2014-09-27 17:49:52

1

平等問題:

Vacation price program Python

提出

考慮它只是像一些改善這個代碼。我認爲它沒有回答你的問題。

我不知道代碼學院爲工作,但某種程度上更容易和更清潔什麼的建議是在下面:

print "this code calculates the total price of a trip, using 4 functions" 

def hotel_cost(nights): 
    return 140 * nights 

def plane_ride_cost(city): 
    #So you can create dict and put for each city 
    #Key - name of city 
    #value - cost 
    CITY_COST = { 
     "Charlotte": 183, 
     "Pittsburgh" : 222, 
     "Los Angeles" : 475, 
     "Tampa": "220" 
    } 
    #Method from dict 
    #if city doesn't exists it'll return False 
    #The second param is default return if doesn't exist key into dict 
    #you can change if do you want 
    return CITY_COST.get(city, False) 



def rental_car_cost(days): 

    cost = days * 40 

    if (days >= 7): 
     cost -= 50 

    elif(days >=3): 
     cost -=20 

    return cost 

def trip_cost(city,days,spending_money): 
    return rental_car_cost(days)+hotel_cost(days)+ plane_ride_cost(city)+spending_money 

city= raw_input("enter city name") 
days= raw_input("enter number of days staying") 
spending_money= raw_input("enter spendig money") 
print trip_cost(city,days, spending_money) 

文檔關於快譯通

https://docs.python.org/2/tutorial/datastructures.html#dictionaries

+0

此代碼具有與我寫的代碼完全相同的問題:(。我不知道我在做什麼錯誤 – user4081147 2014-09-27 05:37:07