jnkim0715 - 1 year ago 98

Python Question

I just started Python 2.7.

I am trying to make a program that executes power function (using loops) without using **import. math**

I think I got it except my program does not execute negative exponents.

The output only comes out as 1.

Here is what I have so far.

`decimal=float(input('Enter the base number:'))`

integer=int(input('Enter the exponent number:'))

def power_function(decimal, integer):

num=1

for function in range(integer):

if integer>0:

num=num*decimal

if integer<0:

num=1/(num*decimal)

return num

print power_function(decimal, integer)

Answer Source

Fixing based on range of negative value.

```
def power_function(decimal, integer):
num=1
if integer>0:
for function in range(integer):
num=num*decimal
if integer<0:
num=1.0 # force floating point division
for function in range(-integer):
num=num/decimal
return num
```