0

Is it possible to easily link each one to the scripts to 'A Complete Formula', as I have to do it multiple times and adding daily?

I have been compiling many of my own python scripts to calculate various math equations. I have over 15 scripts that I want to put together.

I am one of those people who will prefer to have something I can just press and it does the task automatically other than doing in manually - even if it takes twice as long at the start.

I have been just copying and pasting into the 'main' script that I can turn into an .exe but I have found that I have been changing, finding errors or making it better on the original file.

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()
2
  • does the UI for user input exists in this script only, or is it the same in the others too. Commented Jun 1, 2019 at 11:07
  • You can use importlib.import_module() to import (and thereby execute) a script by name. Commented Jun 1, 2019 at 11:09

1 Answer 1

2

You can import the scripts.

Let's say you have the files with the individual formulas as formula1.py, formula2.py, etc with one function in it, solve() that does all the work. Asking for input, calculating, then giving the output. You can then do it like this.

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

If you want your individual files to work as well then you can write them like this:

def solve():
    ...

if __name__ == '__main__':
    solve()

Do take care that without the if __name__ == '__main__' clause, importing the file will also run it.

Sign up to request clarification or add additional context in comments.

2 Comments

Would it work if I did choice == '1': formula1.print(round(ans, rud))
@NicolasHanna formula1 is the name of the file. So formula1.print() will call the print function in the file formula1. By the way, if you have already calculated the answer, then there is no point in importing formula1

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.