Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #55307

Re: Efficency help for a Calculator Program

References <77bde84a-b0ce-4061-bb2e-e6cd23f4282c@googlegroups.com>
Date 2013-10-02 21:01 +1000
Subject Re: Efficency help for a Calculator Program
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.611.1380711666.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Wed, Oct 2, 2013 at 8:44 PM, JonDoe297 <vignesh.harikrishna@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Efficency help for a Calculator Program JonDoe297 <vignesh.harikrishna@gmail.com> - 2013-10-02 03:44 -0700
  Re: Efficency help for a Calculator Program Chris Angelico <rosuav@gmail.com> - 2013-10-02 21:01 +1000
    Re: Efficency help for a Calculator Program JonDoe297 <vignesh.harikrishna@gmail.com> - 2013-10-02 06:43 -0700
  Re: Efficency help for a Calculator Program Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-10-02 19:47 -0400
  Re: Efficency help for a Calculator Program Chris Angelico <rosuav@gmail.com> - 2013-10-03 10:25 +1000
  Re: Efficency help for a Calculator Program Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-10-03 19:15 -0400
  Re: Efficency help for a Calculator Program Chris Angelico <rosuav@gmail.com> - 2013-10-04 16:45 +1000

csiph-web