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

时间:2019-06-01 10:50:19

标签: python

是否可以轻松地将每个脚本都链接到“完整公式”,因为我必须多次执行并每天添加?

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

我是愿意拥有我可以按一下的东西的人之一,它会自动执行任务,而不是手动执行-即使开始时要花两倍的时间。

我只是复制并粘贴到可以转换为.exe的“主”脚本中,但是我发现自己一直在进行更改,发现错误或在原始文件上做得更好。

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

1 个答案:

答案 0 :(得分:1)

您可以导入脚本。

比方说,您拥有文件,其中各个公式分别为formula1.pyformula2.py等,其中包含一个函数solve(),该文件可以完成所有工作。要求输入,计算,然后给出输出。然后,您可以像这样进行操作。

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

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

def solve():
    ...

if __name__ == '__main__':
    solve()

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