[Tutor] Calculate 4**9 without using **
Colin Caine
cmcaine at gmail.com
Sun Mar 5 15:02:34 EST 2017
It's good that you have thought about the different cases, but now your
function is very long.
You can make it a lot shorter and simpler if you think about what
calculations you want to do with different inputs and what calculation can
be shared.
On 5 Mar 2017 19:49, "Sri Kavi" <gvmcmt at gmail.com> wrote:
> I’ve improved it a bit to meet the following conditions:
>
>
>
> 1. type(base) == int and exponent == 0
>
>
>
> 2. base == 0 < exponent
>
>
>
> 3. (base > 0 or base < 0) and exponent > 0
>
>
>
> 4. base > 0 > exponent
>
>
>
> 5. base < 0 > exponent
>
>
>
> 6. base == 0 > exponent
>
>
> def power(base, exponent):
>
> if type(base) == int and exponent == 0:
>
> return 1
>
>
> elif base == 0 < exponent:
>
> return 0
>
>
> elif (base > 0 or base < 0) and exponent > 0:
>
> result = base
>
>
> for _ in range(1, exponent):
>
> result *= base
>
> return result
>
>
> elif base > 0 > exponent:
>
> exponent = -(exponent)
>
> result = base
>
>
> for _ in range(1, exponent):
>
> result *= base
>
>
>
> return 1 / result
>
>
> elif base < 0 > exponent:
>
> exponent = -exponent
>
> result = base
>
>
> for _ in range(1, exponent):
>
> result *= base
>
> return 1 / result
>
>
> elif base == 0 > exponent:
>
> print('0 cannot be raised to a negative power.')
>
>
> #Testing first condition
>
> print(power(0, 0))
>
> print(power(-1, 0))
>
> print(power(1, 0))
>
>
> #Testing second condition
>
> print(power(0, 3))
>
> #Testing third condition
>
> print(power(2, 3))
>
> print(power(-2, 3))
>
>
> #Testing fourth condition
>
> print(power(2, -3))
>
> #Testing fifth condition
>
> print(power(-2, -3))
>
> #Testing sixth condition
>
> print(power(0, -3))
>
>
> I don’t know if it’s anywhere near built-in pow() function, but your reply
> made me think about all those conditions and try to see if I can make my
> previous function code a little better. I need your feedback please.
>
>
> Sri
>
> On Sun, Mar 5, 2017 at 7:37 AM, Alex Kleider <akleider at sonic.net> wrote:
>
> > On 2017-03-04 08:17, Sri Kavi wrote:
> >
> > I'm a beginner learning to program with Python. I'm trying to explain a
> >> solution in plain English. Please correct me if I'm wrong.
> >>
> >
> > Create a function that takes base and exponent as arguments.
> >>
> >
> > Is seems that you are facing the same problem as Tasha Burman.
> > Sounds like an assignment meant to exercise your use of iteration.
> > i.e. ** and various built in power functions that have been suggested are
> > out of bounds.
> >
> > In the body of the function:
> >> set a result variable to the base.
> >>
> >
> > def pwr(base, exponent):
> > ....
> > res = base
> > ...
> >
> >> User a for-loop with a range of 1 to the exponent.
> >>
> >
> > for i in range(begin, end): # The challenge is to pick begin and
> end.
> >
> > end will be a function of exponent but not exponent itself.
> > I don't think 1 is a good choice for begin.
> > Picking the correct begin is related to dealing with the following:
> >
> > What if any of the following are true, and what should be done in each
> > case?
> > if exponent ==1: .....
> > if exponent = 0: .....
> > if exponent < 0: .....
> > Each of the first two might deserve its own return statement.
> >
> >
> >> With each iteration, set the result to the product of result times base.
> >>
> >
> > res *= base # same as res = res * base
> >
> >
> > It's a fun little exercise- a bit more complex than I initially thought
> it
> > would be.
> >
> > Please share your implementation.
> >
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list