首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将多个Python脚本合并为一个脚本?

如何将多个Python脚本合并为一个脚本?
EN

Stack Overflow用户
提问于 2019-06-01 18:50:19
回答 1查看 196关注 0票数 0

有没有可能轻松地将每个公式与脚本链接到“一个完整的公式”,因为我必须多次这样做,每天都要添加?

我一直在编译许多我自己的python脚本来计算各种数学方程。我有超过15个脚本,我想放在一起。

我是那些只需要按一下就可以完成任务的人之一,它会自动完成任务,而不是手动完成-即使一开始需要两倍的时间。

我只是复制和粘贴到‘主’脚本,我可以变成一个.exe,但我发现我一直在改变,发现错误或使其在原始文件上更好。

代码语言:javascript
运行
复制
from math import *
from os import system, name
import math

def clear(): 

    if name == 'nt': 
        _ = system('cls')

while True:
    print("Area of Circle (1)\nArea of a Parallelogram (2)\nArea of a Quadrants and Semicirlces (3)\nArea of a Rectangle (4)\nArea of a Rhombus (5)\nArea of a Trapezium (6)\nArea of a Triangle (7)\nCircumfrence of a Circle (8)\nCircumfrence of a Quadrant or Semicircle (9)\nVolume of a Cylinder (10)\nVolume of a Parallelogram Prism (11)\nVolume of a Rectangular Prism (12)\nVolume of a Rhomus Prism (13)\nVolume of a Sphere (14)\nVolume of a Trapezium Prism (15)\nVolume of a Triangle Prism (16)")
    print()
    choice = input("Choose Formular: ")

    if choice == "1":
        print("Area of a Circle")
        num = float(input("Radius of Circle: "))
        rud = int(input("Place of rounding: "))
        ans = math.pi * num ** int("2")
        print(round(ans, rud))
        input("Press Enter to continue...")
        clear()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-01 19:09:13

您可以导入脚本。

假设您有包含formula1.pyformula2.py等各个公式的文件,其中包含一个函数solve(),它执行所有的工作。要求输入,计算,然后给出输出。然后你就可以这样做了。

代码语言:javascript
运行
复制
import formula1, formula2, formula3
while True:
    ...
    if choice == '1': formula1.solve()
    elif choice == '2': formula2.solve()
    elif choice == '3': formula3.solve()
    else: print('Wrong Choice')

如果您希望您的单个文件也能正常工作,那么您可以这样编写它们:

代码语言:javascript
运行
复制
def solve():
    ...

if __name__ == '__main__':
    solve()

一定要注意,如果没有if __name__ == '__main__'子句,导入文件也会运行它。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56406045

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档