Jump to content
  • Advertisement
Sign in to follow this  
kevtimc

EDIT: My first "real" python program: pycalc

This topic is 4328 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
Just to make sure, did you disable the standard windows firewall, in your security center. It is on by default...

Greetings.

Share this post


Link to post
Share on other sites
Or at the very least, allow IDLE to go through. Sockets connecting to localhost are a common way to do IPC.

Share this post


Link to post
Share on other sites
Quote:
Original post by Limitz
Just to make sure, did you disable the standard windows firewall, in your security center. It is on by default...

Greetings.


Yeah I disabled it.

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
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.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!