I am writing a python code where I ask the user for input and then I have to use their input to give the number of decimal places for the answer to an expression.
Use string formatting and pass userDecimals to the precision part of the format specifier:
userDecimals
precision
>>> import math >>> userDecimals = 6 >>> '{:.{}f}'.format(math.sqrt(1 - .1 **2), userDecimals) '0.994987' >>> userDecimals = 10 >>> '{:.{}f}'.format(math.sqrt(1 - .1 **2), userDecimals) '0.9949874371'