Skip to content

Instantly share code, notes, and snippets.

@aseeon
Created September 4, 2014 13:57
Show Gist options
  • Save aseeon/3f06d95f995fde7adfc2 to your computer and use it in GitHub Desktop.
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
""""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))
@patyyyo
Copy link

patyyyo commented Aug 14, 2019

Athul8raj I've tried using the placeholders in your comment but there not working.

@yashpathare21
Copy link

Please provide an alternative to this return '%.*f' % (n, e) for python 3

@yashpathare21
Copy link

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.

@slysz
Copy link

slysz commented Mar 2, 2021

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.

@yashpathare21
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment