[Tutor] Calculate 4**9 without using **

Sri Kavi gvmcmt at gmail.com
Sat Mar 4 22:06:58 EST 2017


I'm sorry I confused you all. I was trying to reply to Tasha Burman, but I
was in digest mode and I didn't know how to turn it off. So far I've been
just a lurker here. I also don't know if it's a school assignment. Here's
how I would do it.

def power(base, exponent):
    result = base

    for _ in range(1, exponent):
        result *= base

    return result

Also, I'm still coming to grips with Python basics and programming in
general, so I need your feedback.


On Sun, Mar 5, 2017 at 4:26 AM, Alan Gauld via Tutor <tutor at python.org>
wrote:

> On 04/03/17 16: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.
>
> See the thread earlier today with the subject QUESTION for
> more on this topic.
>
> > Create a function that takes base and exponent as arguments.
> >
> > In the body of the function:
> >
> > set a result variable to the base.
> > Use a for-loop with a range of 1 to the exponent.
> > With each iteration, set the result to the product of result times base.
> > After the loop, return the result.
>
> Is there anything there that you don't know how to do?
> If not have a go and if it does not work come back to us
> with your code and any error messages.
>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> _______________________________________________
> 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