用户输入值用作十进制值python [英] user input value to use as decimal value python
问题描述
userDecimals = raw_input(在最终答案中输入您想要的小数位数)
然后将其转换为整数值
userDecimals = int(userDecimals)
然后我写表达式,我想让答案具有与用户输入的小数位数一样多UserDecimals,我不知道如何完成这个。
表达式是
math.sqrt(1 - xx ** 2)
如果不是很清楚,我会尝试更好地解释它,但我是python的新手,而且我不知道该怎么做。
使用字符串格式化,并将 userDecimals
传递给 =http://docs.python.org/2/library/string.html#format-specification-mini-language =nofollow>格式说明符:
>>>导入数学
>>> userDecimals = 6
>>>> '{:。{} f}'。format(math.sqrt(1 - .1 ** 2),userDecimals)
'0.994987'
>>> userDecimals = 10
>>> '{:。{} f}'。format(math.sqrt(1 - .1 ** 2),userDecimals)
'0.9949874371'
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.
userDecimals = raw_input (" Enter the number of decimal places you would like in the final answer: ")
then I convert this to integer value
userDecimals = int(userDecimals)
then I write the expression, and I want the answer to have as many decimal places as the user input from UserDecimals, and I don't know how to accomplish this.
The expression is
math.sqrt(1 - xx **2)
If this isn't clear enough I will try to explain it better, but I am new to python, and I don't know how to do a lot yet.
Use string formatting and pass userDecimals
to the precision
part of the format specifier:
>>> 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'
这篇关于用户输入值用作十进制值python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!