Skip to content

Commit

Permalink
Update power.py
Browse files Browse the repository at this point in the history
  • Loading branch information
teacher-mrdv committed Jun 7, 2022
1 parent 4387aa5 commit 6ae724c
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions python/power.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
def power(base, exponent):
def positive_power(base, exponent):
result = 1
counter = exponent
print(str(base) + "^" + str(exponent) )
while counter > 0:
#print( "result=" + str(result) + 'counter=' + str(counter) )
result = result * base
counter = counter - 1
#print( "result=" + str(result) + 'counter=' + str(counter) )
return result

print( power(2, 8) )
def power(base, exponent):
if exponent < 0:
return 1/positive_power(base, -exponent)
else:
return positive_power(base, exponent)

print( power(2, 8) )
print( power(2, 0) )
print( power(2, -2) )

0 comments on commit 6ae724c

Please sign in to comment.