Created
September 4, 2014 13:57
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""""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)) |
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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Which is easy to read and understand as it don't know anything about python 2.