Home > Software design >  Python function won't print
Python function won't print

Time:02-12

The function looks as follows:

def calc_pi():
    pi = 4
    for i in range (0,200):
        y = 4/(3 (2*i))
        if i % 2 == 0:
            pi -= y
        else:
            pi  = y
        return pi
        print("{}: pi = {}, y = {}".format( i, pi, y))

For some reason Python won't print the result. I don't really know where the problem lies here. I'm absolutely new to programming so it could perfectly well be that I oversee some huge mistakes I've made.

CodePudding user response:

The code below the return can't be reached try this.

def calc_pi():
    pi = 4
    for i in range (0,200):
        y = 4/(3 (2*i))
        if i % 2 == 0:
            pi -= y
        else:
            pi  = y
        print("{}: pi = {}, y = {}".format( i, pi, y))
        return pi

CodePudding user response:

The reason is the line return pi before the print.

This exits the function. Swap the around and it works:

In [1]: def calc_pi():
   ...:     pi = 4
   ...:     for i in range(0, 200):
   ...:         y = 4 / (3   (2 * i))
   ...:         if i % 2 == 0:
   ...:             pi -= y
   ...:         else:
   ...:             pi  = y
   ...:         return pi
   ...:         print("{}: pi = {}, y = {}".format(i, pi, y))
   ...:         

In [2]: calc_pi()
Out[2]: 2.666666666666667

In [3]: def calc_pi():
   ...:     pi = 4
   ...:     for i in range(0, 200):
   ...:         y = 4 / (3   (2 * i))
   ...:         if i % 2 == 0:
   ...:             pi -= y
   ...:         else:
   ...:             pi  = y
   ...:         print("{}: pi = {}, y = {}".format(i, pi, y))
   ...:         return pi
   ...:         

In [4]: calc_pi()
0: pi = 2.666666666666667, y = 1.3333333333333333
Out[4]: 2.666666666666667

Also, your indentation is wrong. As written, your function returns after the first iteration. That it probably not what you want?

It should be:

In [7]: def calc_pi():
   ...:     pi = 4
   ...:     for i in range(0, 200):
   ...:         y = 4 / (3   (2 * i))
   ...:         if i % 2 == 0:
   ...:             pi -= y
   ...:         else:
   ...:             pi  = y
   ...:         print("{}: pi = {}, y = {}".format(i, pi, y))
   ...:     return pi
   ...:     

In [8]: calc_pi()
0: pi = 2.666666666666667, y = 1.3333333333333333
1: pi = 3.466666666666667, y = 0.8
2: pi = 2.8952380952380956, y = 0.5714285714285714
3: pi = 3.3396825396825403, y = 0.4444444444444444
4: pi = 2.9760461760461765, y = 0.36363636363636365
5: pi = 3.2837384837384844, y = 0.3076923076923077
6: pi = 3.017071817071818, y = 0.26666666666666666
7: pi = 3.2523659347188767, y = 0.23529411764705882
8: pi = 3.0418396189294032, y = 0.21052631578947367
...
195: pi = 3.1466687630233094, y = 0.010178117048346057
196: pi = 3.1365421807448284, y = 0.010126582278481013
197: pi = 3.146617747495458, y = 0.010075566750629723
198: pi = 3.136592684838816, y = 0.010025062656641603
199: pi = 3.1465677471829556, y = 0.00997506234413965
Out[8]: 3.1465677471829556
  • Related