我对编码很陌生,所以请耐心等待,但我如何使这段代码有效?我试图获取用户输入的信息并将其输入我的功能,以便我可以打印总数。
def hotel_cost(nights):
return nights * 140
def plane_cost(city):
if city == "Atlanta":
return 220
elif city == "Boston":
return 550
elif city == "Chicago":
return 900
def rental_car_cost(days):
if days >= 7:
return days * 100 - 50
elif days >= 1 and days <= 5:
return days * 100 - 20
a = raw_input("Please choose number of nights: ")
b = raw_input("Please choose which city you are flying to (Atlanta, Boston, Chicago) ")
c = raw_input("Please choose days of rental car use: ")
d = raw_input("Please choose how much spending money you plan on spending: ")
a = nights
b = city
c = days
total = a + b + c + d
print "Your total cost of trip is %d" % total
答案 0 :(得分:0)
我不确定这是否适用于Python版本2.但是,您可以尝试这样做:
a = int(raw_input("Please choose number of nights: ") * 140 )
# Multiplies by 140, but this would defeat the purpose of your functions.
并应用int函数将所有输入转换为整数。
答案 1 :(得分:0)
您需要先将数字输入值转换为数字,然后将输入值传递给函数。
删除行
a = nights
b = city
c = days
使用
计算总数total = hotel_cost(int(a)) + plane_cost(b) + rental_car_cost(int(c)) + float(d)
我认为,对于夜晚和租车日,只有整数才有意义。
答案 2 :(得分:0)
您可以在Python 2中使用input()
,因为它会将输入评估为正确的类型。对于这些函数,您只需将值传递给它们,如下所示:
def hotel_cost(nights):
return nights * 140
def plane_cost(city):
if city == "Atlanta":
return 220
elif city == "Boston":
return 550
elif city == "Chicago":
return 900
def rental_car_cost(days):
if days >= 7:
return days * 100 - 50
elif days >= 1 and days <= 5:
return days * 100 - 20
nights = input("Please choose number of nights: ")
city = raw_input("Please choose which city you are flying to (Atlanta, Boston, Chicago) ")
rental_duration = input("Please choose days of rental car use: ")
spending_money = input("Please choose how much spending money you plan on spending: ")
total = hotel_cost(nights) + plane_cost(city) + rental_car_cost(rental_duration) + spending_money
print "Your total cost of trip is %d" % total