Efficency help for a Calculator Program

2013-10-02 Thread JonDoe297
You may remember me from this : 

https://groups.google.com/forum/#!topic/comp.lang.python/PIkUno3avkw



I need help to increase the efficiency of this code : 


global repeat
repeat=1
def main():
c=int(raw_input("How many numbers do you want to work? (Min. 2 Max. 3) "))
if c==2:
x=int(raw_input("Enter the first number to be worked "))
y=int(raw_input("Enter the second number to be worked "))
elif c==3:
x=int(raw_input("Enter the first number to be worked "))
y=int(raw_input("Enter the second number to be worked "))
z=int(raw_input("Enter the third number to be worked "))
else:
print "Invalid input.";raw_input("Press  to close this 
window");exit()
p=int(raw_input("Do you want to divide, subtract, add or multiply these 
numbers? (1=divide, 2=subtract, 3=add, 4=multiply) "))
if p==1 and c==2:
print "The result is : ",x/y
repeat=int(raw_input("Do you want to calculate for more numbers? Yes=1 
No=2 "))
if repeat==1:
main()
elif p==1 and c==3:
print "The result is : ",x/y/z
repeat=int(raw_input("Do you want to calculate for more numbers? Yes=1 
No=2 "))
if repeat==1:
main()
elif p==2 and c==2:
print "The result is : ",x-y
repeat=int(raw_input("Do you want to calculate for more numbers? Yes=1 
No=2 "))
if repeat==1:
main()
elif p==2 and c==3:
print "The result is : ",x-y-z
repeat=int(raw_input("Do you want to calculate for more numbers? Yes=1 
No=2 "))
if repeat==1:
main()
elif p==3 and c==2:
print "The result is : ",x+y
repeat=int(raw_input("Do you want to calculate for more numbers? Yes=1 
No=2 "))
if repeat==1:
main()
elif p==3 and c==3:
print "The result is : ",x+y+z
repeat=int(raw_input("Do you want to calculate for more numbers? Yes=1 
No=2 "))
if repeat==1:
main()
elif p==4 and c==2:
print "The result is : ",x*y
repeat=int(raw_input("Do you want to calculate for more numbers? Yes=1 
No=2 "))
if repeat==1:
main()
elif p==4 and c==3:
print "The result is : "+str(x*y*z) 
repeat=int(raw_input("Do you want to calculate for more numbers? Yes=1 
No=2 "))
if repeat==1:
main()
else:
repeat=int(raw_input("Invalid Input. Please read instructions properly. 
Would you like to try again? Yes=1 No=2 "))
if repeat==1:
main()
else:
exit()
main()



Is there any way to make it smaller? It does it's job, but I want it to look 
smaller, more efficient.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Efficency help for a Calculator Program

2013-10-02 Thread JonDoe297
On Wednesday, October 2, 2013 4:31:03 PM UTC+5:30, Chris Angelico wrote:
> On Wed, Oct 2, 2013 at 8:44 PM, JonDoe297 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

Thanks a lot again Chris! You understood what I couldn't convey, perfectly! 
I'll use your suggestions ;)
-- 
https://mail.python.org/mailman/listinfo/python-list