Efficency help for a Calculator Program

Chris Angelico rosuav at gmail.com
Wed Oct 2 07:01:03 EDT 2013


On Wed, Oct 2, 2013 at 8:44 PM, JonDoe297 <vignesh.harikrishna at gmail.com> wrote:
> Is there any way to make it smaller? It does it's job, but I want it to look smaller, more efficient.

Yes, it is, but let me first clarify something: "Smaller" and "more
efficient" are two quite different concepts. Efficiency doesn't matter
to your code here, so what you're looking for is smaller, clearer
code. Which is a good thing to be doing :)

At top level, the 'global' declaration doesn't do anything. You may as
well not bother with it.

If you change your recursive main() function into a while loop, you'll
be able to combine all your common code very easily. I won't do the
whole job for you, but consider this structure:

repeat=1
while repeat==1:
    # get inputs
    # calculate and produce output
    repeat=int(raw_input("Do you want to do more? "))

And if you need your error state to have a different prompt, you can
use 'continue' to skip the bottom of the loop.

Hope that helps!

ChrisA



More information about the Python-list mailing list