3

Alright, relevant information can be found in this thread(Is that what they're called here?).

Python Calculator Divide by Zero/Sqrting a Neg. Int. crashing program

Sorry if I should have just kept it to that thread, I'm unfamiliar with the etiquette here and was also unsure if it would be seen.

Anyway, I've made some changes to the code given there. This is my current final product.

import math

def convertString(str):
    try:
        returnValue = int(str)
    except ValueError:
        returnValue = float(str)
    return returnValue

def addition(a, B):
    return convertString(a) + convertString(B)

def subtraction(a, B):
    return convertString(a) - convertString(B)

def multiplication(a, B):
    return convertString(a) * convertString(B)

def division(a, B):
    return convertString(a) / convertString(B)

def sqrt(a):
    return math.sqrt(convertString(a))

def expo(a, B):
    x = convertString(a)
    y = convertString(B)
    return math.pow(x, y)

def fact(a):
    return math.factorial(convertString(a))

keepProgramRunning = True

print "Welcome to [Removed]'s 2011 4-H Project! This is a simple calculator coded in  Python, which is a high-level programming language. Java, C, C++, and Perl are  other high-level programming languages that you may have heard of. Press Enter  to get started!"
print ""
raw_input('')

while keepProgramRunning:
    print "Please select what you would like to do:"
    print ""
    print "1) Addition"
    print "2) Subtraction"
    print "3) Multiplication"
    print "4) Division"
    print "5) Square Root"
    print "6) Exponentiation"
    print "7) Factorial"
    print "8) Quit Program"
    print ""
    print "Input the number of the action that you wish to do here, then press Enter:",
    choice = raw_input()    

    if choice == "1":
        print ""
        numberA = raw_input("Enter the first addend: ")
        numberB = raw_input("Enter the second addend: ")
        print ""
        print "The sum of those numbers is", addition(numberA, numberB)
        print ""
        print "Press the Enter key to continue."
        raw_input('')
    elif choice == "2":
        print ""
        numberA = raw_input("Enter the first term: ")
        numberB = raw_input("Enter the second term: ")
        print ""
        print "The difference of those numbers is", subtraction(numberA, numberB)
        print ""
        print "Press the Enter key to continue."
        raw_input('')
    elif choice == "3":
        print ""
        numberA = raw_input("Enter the first factor: ")
        numberB = raw_input("Enter the second factor: ")
        print ""
        print "The product of those numbers is", multiplication(numberA, numberB)
        print ""
        print "Press the Enter key to continue."
        raw_input('')
    elif choice == "4":
        print ""
        numberA = raw_input("Enter the dividend: ")
        numberB = raw_input("Enter the divisor: ")
        while float(numberB) == 0:
            print ""
            print "You cannot divide by zero. Please choose another divisor."
            print ""
            numberB = raw_input("Enter your divisor: ")
        print ""
        print "The quotient of those numbers is", division(numberA, numberB)
        print ""
        print "Press the Enter key to continue."
        raw_input('')
    elif choice == "5":
        while True:
            print ""
            numberA = raw_input("Enter the number you wish to find the square root of: ")
            if float(numberA) >= 0:
                break
            print ""
            print "You cannot take the square root of a negative number."
        print ""
        print "The square root of that number is", sqrt(numberA)
        print ""
        print "Press the Enter key to continue."
        raw_input('')
    elif choice == "6":
        print ""
        numberA = raw_input("Enter the base: ")
        numberB = raw_input("Enter the exponent: ")
        print ""
        print "The solution to that expression is", expo(numberA, numberB)
        print ""
        print "Press the Enter key to continue."
        raw_input('')
    elif choice == "7":
        while True:
            print ""
            numberA = raw_input("Enter the number you wish to find the factorial of: ")
            if float(numberA) >= 0:
                break
            print ""
            print "You can only find the factorial of non-negative integers."
        print ""
        print "The factorial of that number is", fact(numberA)
        print ""
        print "Press the Enter key to continue."
        raw_input('')
    elif choice == "8":
        print ""
        print "Goodbye! Thank you for your time spent both judging my project and those of     everyone else! Have a nice day! :)"
        print ""
        print "Press the Enter key to close."
        raw_input('')
        keepProgramRunning = False
    else:
        print ""
        print "The key you have selected is not assigned to an action. Please choose from the  listed options."
        print ""
        print "Press the Enter key to continue."
        raw_input('')

I've solved the closing issue, and I've already ran through it to make sure that everything was functional and appearing correctly(Spacer lines where they're supposed to be, no words split between lines, etc.). Now I(believe I) am ready to make it a stand-alone. From what I've seen it's possible, and should even add in anything that's imported(In this case, the math library(I believe that's what it's called.) is imported, so it would be included in the stand-alone version, correct?). So, as my title says, how do I go from a Python file to an executable? I've already tried to find the answer myself, but the tools given are either out of date or don't work(At least how I used them.).

Any advice?

Community
  • 1
  • 1
Sam
  • 75
  • 2
  • 7
  • 1
    It is ok to post another question (even encouraged), but you don't need the to post the full scource code as it is not really relevant to your current question. What would help is explaining how you tried to package the program before. – Jacob Jul 13 '11 at 07:47
  • Ah, alright. Thank you for the advice Cularis. – Sam Jul 13 '11 at 07:48
  • You should use the edit function in your other question to update the first thread. Right now, this question looks like a duplicate of the previous one, and is unclear by itself. The etiquette would be to close this question and update the first one. – Simon Bergot Jul 13 '11 at 08:34
  • @Simon: I would say it only looks like a duplicate if you refuse to read the title and the contents of the post. I realize it is the same project, but it's an entirely different question. The only similarities between this question and my previous one is that it has the information necessary to let it be subject to general feedback. – Sam Jul 13 '11 at 08:53
  • @Sam http://stackoverflow.com/questions/112698/py2exe-generate-single-executable-file was already there :-). I don't refuse to read the question, but I perceived this way: \*title\* \*lot of text and code with no clear relation with the title\* \*any advice?\*. The shorter your question is the easier it is to read. – Simon Bergot Jul 13 '11 at 09:11
  • @Simon: That question is from 2008, and every other board that I've ever been on discourages "necroing", or bumping old topics that haven't been posted in for a while. Arguably, the more information a user gives, the better you can assist them. I clarified upon my issue and the previous steps I have taken. I could have updated my last thread with the new code, but that would have been completely lost. If I had made my question any shorter I don't see how anyone could properly address it. Forgive me for being new to programming and having many questions due to that. – Sam Jul 13 '11 at 09:41
  • @Sam You are right about not bumping very old topics, and not updating your old topic (I got that part wrong, sorry for that). But even if this question is old, the provided answers are still valid. If you have already tested those old answers, you should write what you have tried, and why do you think those solutions are not what you are looking for. I disagree with "the more information..." part. You should provide all the relevant information for your case. But adding a lot of information related to your previous question makes it hard for the reader to find out what is your problem. – Simon Bergot Jul 13 '11 at 10:00
  • @Sam let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/1401/discussion-between-simon-and-sam) – Simon Bergot Jul 13 '11 at 10:01

2 Answers2

6

As you have mentioned other questons and outdated tools (I assume you mean py2exe, last update from 2008), have a look at PyInstaller and its documentation.

Another tool would be cx_freeze.

Jacob
  • 41,721
  • 6
  • 79
  • 81
  • Thank you once again Cularis. That looks fantastic. – Sam Jul 13 '11 at 07:50
  • Can I ask a stupid question? Where do I input stuff such as "python Configure.py"? I've read the documentation over and over until my head hurts, but I'm still completely lost. – Sam Jul 13 '11 at 08:17
  • 1
    Thats a command, you run it by opening a command box: 1. `Start > Run ... ` 2. Enter `cmd` 3. Change into your directory where Configure.py is located 4. Run `python Configure.py` – Jacob Jul 13 '11 at 08:19
  • I navigated to the directory(I know how to do that much.) but whenever I input the command given, it just says that "python" is not a recognized command. I hope I'm not being too much of a burden. I frequent the WoW Customer Support Forums and I'm quite familiar with the urge to bang your head against the desk due to users who can't bother themselves to look anything on their own, and I hope I'm not giving you a similar urge. – Sam Jul 13 '11 at 08:29
  • 2
    No problem. What that means is that the python directory is not in your `PATH` environemnt variable. [Here is a link describing how to add a path to your PATH](http://geekswithblogs.net/renso/archive/2009/10/21/how-to-set-the-windows-path-in-windows-7.aspx). If you don't feel comfortable editing the System Path, call python with the fullpath: `C:\path\to\python\python.exe Configure.py` – Jacob Jul 13 '11 at 08:32
  • Tried both methods, neither work. I try the first one, and it still says that python isn't a recognized command. I tried both adding the python directory and the python.exe. The latter method wants me to install PythonWin, so I did and it still requests it to be installed. It's the latest and correct version. – Sam Jul 13 '11 at 08:51
  • executing python.exe in your shell should open a python interpreter. does that happen? – Jacob Jul 13 '11 at 08:55
  • @Sam let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/1399/discussion-between-cularis-and-sam) – Jacob Jul 13 '11 at 08:55
1

Py2exe always worked for me. I had made an exe out of a script using PIL, and it worked without problems. The documentation is good, and I was able to have it packaged in matter of minutes.

Senthess
  • 17,020
  • 5
  • 23
  • 28
  • I've tried that, but the problem with it is that it is incompatible with my version of Python. I'm on 2.7.2, and that only supports up to 2.5. Py2exe hasn't been updated for three years. – Sam Jul 13 '11 at 07:47
  • 1
    @Sam Who told you that? Py2exe is currently up to date and has build for Python 2.7. Look here http://sourceforge.net/projects/py2exe/files/py2exe/0.6.9/ – Vader Jul 13 '11 at 08:09
  • @Vader: That would be my bad for assuming the latest version would be at the top of the list. >. – Sam Jul 13 '11 at 08:32