关于`function`的正确用法

时间:2015-12-24 12:43:22

标签: function python-2.7

以下代码块似乎无法正常运行(更具体地说,它没有打印出字符串),我想知道导致错误的是什么。

我假设它与我在最后一行调用函数的方式有关,也许?我希望return980函数hotel_cost(nights) print hotel_cost(nights),以便980打印出def hotel_cost(nights): """ Calculate the total staying cost, based on how many days you're looking to stay. $140 per night. """ total = 140 * nights print "The total cost of staying %s nights is: %s" % (nights, total) return hotel_cost(7)

SELECT  account_code, SUBSTRING(account_code ,5,3 ) as first_level_code 
  from FROM acc_coa  
  where SUBSTRING(account_code ,5,3 ) = (SELECT MAX(SUBSTRING(account_code ,5,3 )) as first_level_code 
 FROM acc_coa 
 WHERE category = '1') ;

1 个答案:

答案 0 :(得分:2)

首先,你必须创建一个返回酒店总费用的函数。

然后从外面调用该函数。您通常不会从其中调用函数的名称(称为递归和更高级的技术。)

def hotel_cost(nights):
    """ Calculate the total staying cost,
    based on how many days you're looking to stay. $140 per night. """

    return 140 * nights

# Outside the function
nights = 7
total = hotel_cost(nights)
print "The total cost of staying %s nights is: %s" % (nights, total)