-
-
Save aseeon/3f06d95f995fde7adfc2 to your computer and use it in GitHub Desktop.
""""Find e to the Nth Digit | |
Enter a number and have the program generate e up to that many decimal places. | |
Keep a limit to how far the program will go""" | |
from math import e | |
def e_with_precision(n): | |
"""Return euler's number to the n-th decimal place | |
:param n: number of decimal places to return | |
:type n: int | |
:return: euler's number with n decimal places | |
:rtype: str | |
""" | |
return '%.*f' % (n, e) | |
if __name__ == '__main__': | |
# there is no do while loop in python, so we need to improvise | |
correct_input = False | |
while not correct_input: | |
# ask until you get correct input | |
print('Precision must be between 1 and 51') | |
precision = int(raw_input('Number of decimal places: ')) | |
if 51 >= precision > 0: | |
correct_input = True | |
print(e_with_precision(precision)) |
The output is incorrect for n > 15
.
https://www.math.utah.edu/~pa/math/e.html.
.....Your output for n=50
with spaces added: 2.71828 18284 59045 09079 55982 98427 64884 23347 47314 45312
Correct output for n=50
with spaces added: 2.71828 18284 59045 23536 02874 71352 66249 77572 47093 69995
return '%.*f' % (n,e).
hi, I tried to use the new formatting for this but I could not get it right, i'm new to this, so I was wondering if there is way to use the new formatting in python 3 instead of %.
thank you in advance
You can put placeholders "{ :*f}."format(n,e) in python 3 or using f string formatting in python 3.6...both works fine
Athul8raj I've tried using the placeholders in your comment but there not working.
Please provide an alternative to this return '%.*f' % (n, e) for python 3
Please provide an alternative to this return '%.*f' % (n, e) for python 3
Which is easy to read and understand as it don't know anything about python 2.
Please provide an alternative to this return '%.*f' % (n, e) for python 3
This works perfectly well on Python 3. So just learn how different formatting styles work -> https://pyformat.info/ and rewrite it yourself.
I meant can you give an alternative to this return '%.*f' % (n, e) in .format() or f-string method. As I know how these methods work.
return '%.*f' % (n,e) , returns the string type of nth value of e ,toned down to precision n. When the precision is not known, until before runtime, this syntax works fine in that case.