Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions PI_CalculationBlock.py
Original file line number Diff line number Diff line change
@@ -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')
38 changes: 38 additions & 0 deletions PI_TestScript.py
Original file line number Diff line number Diff line change
@@ -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]}')
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# The worst way to calculate π in Python
# Add more info

### This project shows what should be avoided when creating Python projects.

### There are many bad practises there, can you find (and correct) them all?

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