递归函数使a ^ b

时间:2018-08-24 08:32:53

标签: python

我的递归函数似乎不起作用-应该采用参数ab并返回 a^b

这里是:

def power(a, b):
       If b == 0:
               return
       return a * power(a,  b-1)
power(2,  3)

2 个答案:

答案 0 :(得分:4)

基本案例的返回语句为null。您应该返回1(因为a^0 = 1的{​​{1}})。

a != 0

由于if b == 0: return 1 在数学上是未定义的,因此您可能要分别处理a = 0的情况:

0^0

答案 1 :(得分:0)

+----------+-------+--------------------+-----------+--------------+--------------------+--------------------+--------------------+-------------+------------+
|      asin|overall|          Reviewtext| reviewTime|    reviewerID|        reviewerName|             summary|      unixReviewTime|useful_review|voted_review|
+----------+-------+--------------------+-----------+--------------+--------------------+--------------------+--------------------+-------------+------------+
|B000F83SZQ|    5.0|I enjoy vintage b...| 05 5, 2014|A1F6404F1VG29J|          Avidreader|  Nice vintage story|1399248000       ...|            0|           0|
|B000F83SZQ|    4.0|This book is a re...| 01 6, 2014| AN0N05A9LIJEQ|            critters|        Different...|1388966400       ...|            2|           2|
|B000F83SZQ|    4.0|This was a fairly...| 04 4, 2014| A795DMNCJILA6|                 dot|               Oldie|1396569600       ...|            2|           2|
|B000F83SZQ|    5.0|I'd never read an...|02 19, 2014|A1FV0SX13TWVXQ|Elaine H. Turley ...|  I really liked it.|1392768000       ...|            1|           1|
|B000F83SZQ|    4.0|If you like perio...|03 19, 2014|A3SPTOKDG7WBLN|  Father Dowling Fan|      Period Mystery|1395187200       ...|            0|           1|
|B000F83SZQ|    4.0|A beautiful in-de...|05 26, 2014|A1RK2OCZDSGC6R|    ubavka seirovska|              Review|1401062400       ...|            0|           0|
|B000F83SZQ|    4.0|I enjoyed this on...|06 10, 2014|A2HSAKHC3IBRE6|            Wolfmist|Nice old fashione...|1402358400       ...|            0|           0|
|B000F83SZQ|    4.0|Never heard of Am...|03 22, 2014|A3DE6XGZ2EPADS|                 WPY|Enjoyable reading...|1395446400       ...|            1|           1|
|B000FA64PA|    5.0|Darth Maul workin...|10 11, 2013|A1UG4Q4D3OAH3A|                 dsa|          Darth Maul|1381449600       ...|            0|           0|
|B000FA64PA|    4.0|This is a short s...|02 13, 2011| AQZH7YTWQPOBE|            Enjolras|Not bad, not exce...|1297555200       ...|            0|           0|
|B000FA64PA|    5.0|I think I have th...|01 27, 2014|A1ZT7WV0ZUA0OJ|                Mike|      Audio and book|1390780800       ...|            0|           0|
|B000FA64PA|    4.0|Title has nothing...|09 17, 2011|A2ZFR72PT054YS|          monkeyluis|Darth Maul...the ...|          1316217600|            0|           0|
+----------+-------+--------------------+-----------+--------------+--------------------+--------------------+--------------------+-------------+------------+