我正在尝试使用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 :(得分:1)
仅考虑一下改进此代码。我想是的,它没有回答你的问题。
我不知道Code Academy对于该练习的建议是什么,但在某种程度上更容易和更清洁:
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
答案 1 :(得分: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)