从 .py 到 .exe

发布于 2024-11-19 13:32:43 字数 5317 浏览 2 评论 0原文

好的,相关信息可以在这个帖子中找到(这里是这么叫的吗?)。

Python 计算器除以零/求负数平方。国际。程序崩溃

抱歉,如果我应该将其保留在该线程中,我不熟悉这里的礼仪,也不确定它是否会被看到。

无论如何,我对那里给出的代码做了一些更改。这是我目前的最终产品。

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('')

我已经解决了结束问题,并且已经完成了它以确保一切功能正常并且显示正确(间隔线应该在它们应该在的位置,行之间没有单词分割,等等)。现在我(相信我)已经准备好将其作为独立的作品。据我所知,这是可能的,甚至应该添加任何导入的内容(在这种情况下,数学库(我相信这就是它的名字)被导入,所以它将包含在独立版本中,正确?)。那么,正如我的标题所说,如何从 Python 文件转换为可执行文件?我已经尝试自己找到答案,但是给出的工具要么已经过时,要么不起作用(至少我是如何使用它们的)。

有什么建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沉睡月亮 2024-11-26 13:32:43

正如您提到的其他问题和过时的工具(我假设您指的是 py2exe,上次更新是 2008 年),请查看 PyInstaller 及其文档

另一个工具是cx_freeze

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.

成熟的代价 2024-11-26 13:32:43

Py2exe 总是对我有用。我使用 PIL 从脚本中制作了一个 exe,它运行没有问题。文档很好,我可以在几分钟内将其打包。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文