感谢任何帮助,还有任何重大缺陷或您在格式或基本方面看到的任何重大缺陷,请指出。谢谢!
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
to an int
,所以你需要更改顶部部分。
day = raw_input("How many days?")
location = raw_input("Where to?")
days = int(day)
spendingMoney = 100
在你的原始代码中,days
是一个字符串,因此您尝试将字符串添加到整数(这引发了错误)。
将字符串乘以整数是完全有效的,因为它只是将原始字符串重复几次。
print 'foobar' * 5
# foobarfoobarfoobarfoobarfoobar
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)