感谢
任何帮助,也欢迎您在格式化方式中看到的任何大缺陷或基本内容,请指出。谢谢!
day = raw_input("How many days?")
locations = raw_input("Where to?")
days = str(day)
location = str(locations)
spendingMoney = 100
def hotel(days):
return 140 * days
def destination(location):
if location == "los angeles":
return 300
if location == "boston":
return 400
def rental(days):
if days < 2:
return 40 * days
if days >= 2 and days <= 6:
return days * 30
if days >= 7:
return days * 25
def total_cost(days, location):
return hotel(days) + destination(location) + rental(days)
print total_cost(days, location)
首先要了解的是raw_input
返回一个字符串,因此之后无需将结果转换为字符串。
你想要的(我认为)是将day
投射到int
,所以你需要改变顶部。
day = raw_input("How many days?")
location = raw_input("Where to?")
days = int(day)
spendingMoney = 100
在您的原始代码中,days
是一个字符串,因此您尝试将字符串添加到 和 整数(这引发了错误)。
乘以整数是完全有效的,因为它只是将原始字符串重复几次。
print 'foobar' * 5
# foobarfoobarfoobarfoobarfoobar
问题是days
是一个字符串。
当你这样做时
return 140 * days
它实际上将您的字符串乘以 140。因此,如果days
=="5",您将有"555555555555555555..."(140 个字符)
你想用整数操作,所以做 days = int(day)