我正在编写一个python代码,在这里我要求用户输入,然后我必须使用他们的输入给出表达式答案的小数位数。在
userDecimals = raw_input (" Enter the number of decimal places you would like in the final answer: ")
然后我把它转换成整数值
^{pr2}$然后我编写表达式,我希望答案的小数位数与用户从UserDecimals输入的一样多,但我不知道如何实现这一点。在
表达式是
math.sqrt(1 - xx **2)
如果这还不够清楚,我将尝试更好地解释它,但我是python新手,我还不知道如何做很多事情。在
格式化打印语句时,可以指定要显示的有效数字的数目。例如,
'%.2f' % float_value
将显示两个小数位。有关更详细的讨论,请参见this question。在你想要这样的东西:
输出:
^{pr2}$使用string formatting并将
userDecimals
传递给format specifier的precision
部分:相关问题 更多 >
编程相关推荐