diff --git a/PI_CalculationBlock.py b/PI_CalculationBlock.py new file mode 100644 index 0000000..8855054 --- /dev/null +++ b/PI_CalculationBlock.py @@ -0,0 +1,34 @@ +class PiContainer: + def __init__(self, a=None): + self.a = a if a is not None else [] + + def mth(self, x): + if isinstance(x, list): + self.a += x + else: + self.a.append(x) + + +def generate_pi_approximations(iterations): + total = 0 + divisor = 1 + + for iteration_number in range(iterations): + try: + if iteration_number % 2 == 0: + total += 4 / divisor + else: + total -= 4 / divisor + except ZeroDivisionError as e: + yield f'Error: {e}' + else: + divisor += 2 + yield total + yield 'finished' + +def enumerate_pi(pi: PiContainer): + for approximation in pi.a: + print(f'Next approximation: {approximation}') + +if __name__ == '__main__': + print('All functions are defined') \ No newline at end of file diff --git a/PI_TestScript.py b/PI_TestScript.py new file mode 100644 index 0000000..842ffb5 --- /dev/null +++ b/PI_TestScript.py @@ -0,0 +1,38 @@ +from PI_CalculationBlock import PiContainer, generate_pi_approximations + +NUM_GENERATOR_CALLS = 194 +NUM_ITERATIONS = 23 + +def my_enumerate(pi): + for actualPI in pi.a: + print(f'Next element: {actualPI}') + +if __name__ == '__main__': + try: + my_pi = PiContainer() + pi_gen = generate_pi_approximations(5) + for _ in range(5): + my_pi.mth(next(pi_gen)) + my_pi_2 = PiContainer() + for _ in range(3): + my_pi_2.mth(next(pi_gen)) + except StopIteration: + print('Generator has completed.') + + pi_gen3 = generate_pi_approximations(NUM_GENERATOR_CALLS) + for _ in range(NUM_ITERATIONS): + try: + my_pi_2.mth(next(pi_gen3)) + except StopIteration: + print('Generator has completed.') + + my_pi_3 = PiContainer() + pi_gen = generate_pi_approximations(6) + my_pi_3.mth(list(pi_gen)) + print('my first pi') + my_enumerate(my_pi) + print('my second pi') + my_enumerate(my_pi_2) + + with open('result_Pi_Calculation.txt', 'w') as new_file: + new_file.write(f'my best pi: {my_pi_3.a[-1]}') \ No newline at end of file diff --git a/README.md b/README.md index 5fdbab8..a0b694f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # The worst way to calculate π in Python +# Add more info ### This project shows what should be avoided when creating Python projects. @@ -6,4 +7,4 @@ Your task is to clone this repository, create a new branch, fix all the issues and bad practises, add file with list of all the changes and explanations why they are wrong. Then create a pull request with your branch, add your teacher as reviewer. -(As this project is full of bad practises please do not use it as an example. It's for educational purposes only.) +(As this project is full of bad practises please do not use it as an example. It's for educational purposes only.)