Sign in to follow this  
kevtimc

EDIT: My first "real" python program: pycalc

Recommended Posts

While working in python, when I try to run a module in the python shell (dled off python.org), I will get an error "IDLE's subprocess didn't make connection. Either IDLE can't start a subprocess or personal firewall software is blocking the connection.". This occurs even with all firewalls off, help please =( [Edited by - kevtimc on September 7, 2006 6:36:47 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by kevtimc
Yeah I disabled it.


Well, in that case you need to check whether you have a runaway python subprocess left behind by a previous session. Check your process list and kill off any python.exe you see there.

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
Quote:
Original post by kevtimc
Yeah I disabled it.


Well, in that case you need to check whether you have a runaway python subprocess left behind by a previous session. Check your process list and kill off any python.exe you see there.


I think that might have been my problem, with that being said here's my first "real" python program, pycalculator:


def displaywelcome():
print "-----------------------------------"
print "|_________________________________|"
print "|_________________________________|"
print "|Welcome to the python calculator!|"
print "|_________________________________|"
print "| |"
print "|---------------------------------|"
print "| |"
print "| ------- ------- ------- |"
print "| | 9 | | 8 | | 7 | |"
print "| ------- ------- ------- |"
print "| ------- ------- ------- |"
print "| | 6 | | 5 | | 4 | |"
print "| ------- ------- ------- |"
print "| ------- ------- ------- |"
print "| | 3 | | 2 | | 1 | |"
print "| ------- ------- ------- |"
print "| ------- ------- ------- |"
print "| |Enter| |Clear| | Quit | |"
print "| ------- ------- ------- |"
print "|_________________________________|"

def updatedisplay(number):
print "-----------------------------------"
print "|_________________________________|"
print "|_________________________________|"
print "| ",number, " |"
print "|_________________________________|"
print "| |"
print "|---------------------------------|"
print "| |"
print "| ------- ------- ------- |"
print "| | 9 | | 8 | | 7 | |"
print "| ------- ------- ------- |"
print "| ------- ------- ------- |"
print "| | 6 | | 5 | | 4 | |"
print "| ------- ------- ------- |"
print "| ------- ------- ------- |"
print "| | 3 | | 2 | | 1 | |"
print "| ------- ------- ------- |"
print "| ------- ------- ------- |"
print "| |Enter| |Clear| | Quit | |"
print "| ------- ------- ------- |"
print "|_________________________________|"

displaywelcome()
number1 = input("Please enter a number")
updatedisplay(number1)
operation = raw_input("What operand do you wish to use? (addition, subtraction, multiplication, or division)")
if operation != "addition" and operation != "subtraction" and operation != "multiplication" and operation != "division":
print "Please enter one of the valid choices"
operation = raw_input("What operand do you wish to use? (addidtion, subtraction, multiplication, or division)")
number2 = input("Please enter the last number")
updatedisplay(number2)

if operation == "addition":
answer = number1 + number2
elif operation == "subtraction":
answer = number1 - number2
elif operation == "multiplication":
answer = number1 * number2
elif operation == "division":
answer = number1 / number2

print "Answer:"
updatedisplay(answer)




The error check is primitive because I haven't learned about loops yet, but its a calculator nonetheless.

Share this post


Link to post
Share on other sites
Looks reasonable. You can probably change this line:

if operation != "addition" and operation != "subtraction" and operation != "multiplication" and operation != "division"

To this line:

if operation not in ("addition", "subtraction", "multiplication", "division"):

Share this post


Link to post
Share on other sites
Although I think this might be a little advanced for you right now, it never hurts to check out what the language can really do. You can substitute the long 'if' chains:


if operation == "addition":
answer = number1 + number2
elif operation == "subtraction":
answer = number1 - number2
elif operation == "multiplication":
answer = number1 * number2
elif operation == "division":
answer = number1 / number2




with:


operations={'addition':lambda x,y: x+y,
'subtraction':lambda x,y: x-y,
'multiplication':lambda x,y: x*y,
'division':lambda x,y: x/y}
answer=operations[operation](num1,num2)




Basically, we create a dictionary that stores lambdas(think of them as unnamed functions defined on-the-fly). We can then access the desireable operation using the string "operation" as key.

Share this post


Link to post
Share on other sites
Quote:
Original post by tldalton1622
hey im learning python too. send me a pm if you want to exchange IM screen names. We can bounce ideas off each other if you'd like. everyone else is doing the whole c++ thing.


Which handle do you want?

Share this post


Link to post
Share on other sites
Quote:
Original post by mikeman
Although I think this might be a little advanced for you right now, it never hurts to check out what the language can really do. You can substitute the long 'if' chains:

*** Source Snippet Removed ***

with:

*** Source Snippet Removed ***

Basically, we create a dictionary that stores lambdas(think of them as unnamed functions defined on-the-fly). We can then access the desireable operation using the string "operation" as key.


This sacrifices some more error checking, but you can also make use of Python's runtime evaluation, like so (which also has an advantage from a UI perspective, because you don't expect the user to type out full words and spell them correctly ;) ):


answer = eval('lambda x, y: x %s y' % operation)(num1, num2)


That is, substitute the operation string (which should now be one of the appropriate symbols) into a longer string (which just happens to be Python code to create one of those lambdas), interpret the string as a Python expression (not a statement! You couldn't eval() an if-construct or print statement for example; use 'exec' for that, but you won't get a return value), so as to create the appropriate lambda function, then call the function with the arguments. [smile]

Of course, the whole exercise is more or less moot because the Python command line is already a calculator:


Python 2.3.2 (#49, Oct 2 2003, 20:02:00) [MSC v.1200 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 6 * 9
54
>>> # Not 42, apologies to DNA fans


Of course, because it's interpreting your input as Python code rather than just input to a calculator, it's quite a bit more powerful. So the real exercise in making a calculator program is *restricting* things ;) Oh, and modelling the interface that a real calculator provides. Try playing around with the Windows Calculator, or a real one, for example.

By the way, you can get access to the "command line calculator" from within a program by simply using input() instead of raw_input(); input() should basically be equivalent to eval(raw_input()).

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Quote:
Original post by mikeman
Although I think this might be a little advanced for you right now, it never hurts to check out what the language can really do. You can substitute the long 'if' chains:

*** Source Snippet Removed ***

with:

*** Source Snippet Removed ***

Basically, we create a dictionary that stores lambdas(think of them as unnamed functions defined on-the-fly). We can then access the desireable operation using the string "operation" as key.


This sacrifices some more error checking, but you can also make use of Python's runtime evaluation, like so (which also has an advantage from a UI perspective, because you don't expect the user to type out full words and spell them correctly ;) ):


answer = eval('lambda x, y: x %s y' % operation)(num1, num2)




Yeah, I didn't want to mention eval() since that makes things too easy - I understand he's doing this to learn. Heck, with eval you don't even need to create lambdas, you could just do:

answer = eval('num1 %s num2' % operation)

Share this post


Link to post
Share on other sites
There's no need to bother with lambdas: just use the operator module:

import operator

operations = {
'addition' : operator.add,
'subtraction' : operator.sub,
'multiplication' : operator.mul,
'division' : operator.truediv # bypasses integer division issues
}

answer = operations[operation](num1,num2)


Quote:
Python 2.3.2 (#49, Oct 2 2003, 20:02:00) [MSC v.1200 32 bit (Intel)] on win32


2.3? How quaint. [grin]

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
There's no need to bother with lambdas: just use the operator module:

import operator

operations = {
'addition' : operator.add,
'subtraction' : operator.sub,
'multiplication' : operator.mul,
'division' : operator.truediv # bypasses integer division issues
}

answer = operations[operation](num1,num2)


Yeah, and you can also get at it with e.g. float.__add__ etc. (although that restricts the input type). And they say TMTOWTDI is for Perl hackers... [smile]

Quote:
Quote:
Python 2.3.2 (#49, Oct 2 2003, 20:02:00) [MSC v.1200 32 bit (Intel)] on win32


2.3? How quaint. [grin]


Yeah, so I haven't updated; sue me. I'm probably running a positively ancient version of Firefox, too. [smile]

Share this post


Link to post
Share on other sites
[quote]Original post by Zahlman
Yeah, and you can also get at it with e.g. float.__add__ etc. (although that restricts the input type).

Ah, but using operator is the obvious way... assuming you know the module is there. Given that it is a built-in module, I suspect it might give better results than a lambda, performance-wise.

Quote:
And they say TMTOWTDI is for Perl hackers... [smile]

Quote:
From the Zen of Python
There should be one-- and preferably only one --obvious way to do it.


import this [smile]

Quote:
Yeah, so I haven't updated; sue me. I'm probably running a positively ancient version of Firefox, too. [smile]


You should hear from my lawyer shortly.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this