#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Posts
    54
    Rep Power
    9

    How to insert a new line character in the middle of a string returned from a function


    I wrote a function to calculate pi based on the following formula:

    4/1 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + ...

    I want to give this function an integer, which is the number of terms that it calculates in the series, e.g. 'calc_pi(100)". Then the function will return a string like this:

    "The value of pi that you calculated based on 100 terms in the series is 3.14159.
    The difference between this value and the value calculated by the math library is 0.000023."

    I'm having trouble with the new line between the two sentences. I'm trying stuff like this in my function definition, but the new line shows up as '\n' rather than a new line.

    Code:
    answer = "Your pi, which was calculated with " + str(n) + " numbers is " + str(pi) + ".\n"
    
    answer = answer + "The difference between your pi and the one in the math library is " + str(diff) + '.'
    
    return answer
    Can someone point me in the right direction?

    Thank you.

    Eric
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    451
    Rep Power
    32
    That is correct. If you print the string "answer" you will see that it prints with the new line.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    terribly sorry, cannot find a problem in either python 2 or python 3.

    Code:
    >>> def f(n,pi,diff):
    ...  answer = "Your pi, which was calculated with " + str(n) + " terms is " + str(pi) + ".\n"
    ...  answer += "The difference between your pi and the one in the math library is " + str(diff) + '.'
    ...  return answer
    ... 
    >>> import math
    >>> print(f(34,math.pi,0))
    Your pi, which was calculated with 34 terms is 3.14159265359.
    The difference between your pi and the one in the math library is 0.

    OH! Maybe you didn't print the function result.

    Code:
    >>> f(34,math.pi,0)
    'Your pi, which was calculated with 34 terms is 3.141592653589793.\nThe difference between your pi and the one in the math library is 0.'
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Posts
    54
    Rep Power
    9
    Hi dwblas and b49P23TIvg,

    If I do everything in the shell, then it works fine (i.e. if I don't create a function and simply build up the elements of 'answer' and then print it):

    Code:
    >>> print(answer)
    You're pi calculated with 99999 numbers is 3.1416026536897204.
    The difference between your pi and the one in the math library is 1.0000099927243156e-05.
    But if I define a function called 'calc_pi' to calculate pi and then build up the elements of 'answer' there and return 'answer', the new line character shows up as '\n' rather than showing up as a new line (I used a different 'n', so the numbers are a bit different):

    Code:
    >>> calc_pi(4444)
    "You're pi calculated with 4444 numbers is 3.141367631090394.\nThe difference between your pi and the one in the math library is 0.0002250224993991523."
    The two outputs look different and I don't like the version that is returned from my 'calc_pi' function. For what it's worth, the complete function is pasted below.

    Thanks so much for the help.

    Eric


    Code:
    
    def calc_pi(n):
        '''(int) -> str
    
        Calculate an approximation of pi based on how many terms to sum in the following series:
        4/1 - 4/3 + 4/5 - 4/7 + 4/9 - ...
        >>> calc_pi(200)
        >>> 3.136592684838816
        >>> calc_pi(200000)
        >>> 3.1415876535897618
        >>> calc_pi(20)
        >>> 3.09162380666784
        '''
    
        denom = 1
        pi = 0
        sign = 1
        for i in range(n):
            pi = pi + (sign * 4) / denom
            denom = denom + 2
            sign = sign * -1
        import math
        diff = abs(math.pi - pi)
        answer = "You're pi calculated with " + str(n) + " numbers is " + str(pi) + ".\n"
        answer = answer + "The difference between your pi and the one in the math library is " + str(diff) + '.'
        return answer
    Last edited by EJF; November 18th, 2012 at 09:12 PM.
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    Both of us told you to print the function result!

    >>> print(f(x))
    line 1
    line 2
    >>> v = f(x)
    >>> print(v)
    line 1
    line 2
    >>> v
    'line 1\nline 2'
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Posts
    54
    Rep Power
    9
    Hi dwblas and b49P23TIvg,

    Thanks very much. I now understand. Sorry for being so dense.

    Eric

IMN logo majestic logo threadwatch logo seochat tools logo