【发布时间】:2016-11-26 01:54:59
【问题描述】:
以下是计算用户假期费用的三个函数。鼓励用户输入他的假期的详细信息,然后将其作为参数传递给函数。
def hotel_cost(days):
days = 140*days
return days
"""This function returns the cost of the hotel. It takes a user inputed argument, multiples it by 140 and returns it as the total cost of the hotel"""
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
"""this function returns the cost of a plane ticket to the users selected city"""
def rental_car_cost(days):
rental_car_cost=40*days
if days >=7:
rental_car_cost -= 50
elif days >=3:
rental_car_cost -= 20
return rental_car_cost
"""this function calculates car rental cost"""
user_days=raw_input("how many days would you be staying in the hotel?") """user to enter a city from one of the above choices"""
user_city=raw_input("what city would you be visiting?") """user to enter number of days intended for holiday"""
print hotel_cost(user_days)
print plane_ride_cost(user_city)
print rental_car_cost(user_days)
我注意到,当我打印上面的函数时,只有 plane_ride_cost(user_city) 可以正常运行。其他两个函数吐出乱码。这是为什么呢?
【问题讨论】:
-
raw_input返回一个字符串,而不是一个整数 -
因为您没有任何“int 类型变量”。你只有字符串。
标签: python python-2.7