How to use string formatting specification to control decimals using inputs

Viewed 22

I was wondering why there is an error using a variable inside of the f statement and how I could fix it.

The code:

from math import *
precision = float(input("Enter digits of decimals:"))
print(f'The value of pi to {precision} digits is: {pi:.{precision}f}')

And the error:

    print(f'The value of pi to {precision} digits is: {pi:.{precision}f}')
ValueError: Invalid format specifier
1 Answers

Precision cannot be float, needs to be int

from math import *
precision = int(input("Enter digits of decimals:"))
print(f'The value of pi to {precision} digits is: {pi:.{precision}f}')
Related