Note that there are some explanatory texts on larger screens.

plurals
  1. PO**recurPower** I got it but I dont understand it
    primarykey
    data
    text
    <p>Ok, let me start by saying yes this is homework. But I have the answer (played with it til it worked) my question is more on "how" I have had the teacher explain it multiple times (online class) but I am just not getting it, hoping that someone on here is better at explaining things the way I think about them.</p> <p>Here is the assignment:</p> <p>Write a function <code>recurPower(base, exp)</code> which computes <code>base**exp</code> by recursively calling itself to solve a smaller version of the same problem, and then multiplying the result by <code>base</code> to solve the initial problem.</p> <p>This function should take in two values - <code>base</code> can be a float or an integer; <code>exp</code> will be an integer <strong>≥0</strong>. It should return one numerical value. Your code must be recursive - use of the <code>**</code> operator or looping constructs is not allowed.</p> <p>Ok, so after a few attempts with trial and error (by this I mean a few hours of changing things wildly) I came up with the correct code and it solved for the correct answers, but I dont understand how. </p> <p>Here is the code:</p> <pre><code>def recurPower(base, exp): ''' base: int or float. exp: int &gt;= 0 returns: int or float, base^exp ''' if exp &lt;= 0: return 1 return base * recurPower(base, exp - 1) </code></pre> <p>First thing is the part where exp = 0 then return is 1.... I dont understand why anything would come back with 1. Second is if the final part of the code, if there is no loop where does the exp drop by 1?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload