Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 打印中的右对齐错误()_Python_Python 3.x_Printing_Alignment - Fatal编程技术网

Python 打印中的右对齐错误()

Python 打印中的右对齐错误(),python,python-3.x,printing,alignment,Python,Python 3.x,Printing,Alignment,我试图将从打印中获得的数值右对齐,但出现错误: 'float' object has no attribute 'rjust' 这是我试图使用它的代码。我应该如何正确地将我得到的所有数值与print对齐: enterValue = float(input("The input value is: ")) # We know that the circumference of a circle is 2*pi*r. CircumferenceV = (2*math.pi*enterValue)

我试图将从打印中获得的数值右对齐,但出现错误:

'float' object has no attribute 'rjust'
这是我试图使用它的代码。我应该如何正确地将我得到的所有数值与
print
对齐:

enterValue = float(input("The input value is: "))

# We know that the circumference of a circle is 2*pi*r.
CircumferenceV = (2*math.pi*enterValue)
print("The Circumference is : %0.2f" %(circleCircumference.rjust(10)))

当为
str
对象定义了
rjust
时,您将在
浮点上应用
rjust
。另一种方法是右对齐以下位置的
:fill.precision
符号:

应用于
电路循环

print("The Circumference is : {:10.2f}".format(circleCircumference))
仅使用
%
可以对以下各项执行相同的操作:

>>> circleCircumference = 3.29920
>>> "Circle circumfence: %10.2f" % circleCircumference
'Circle circumfence:        3.30'

当为
str
对象定义了
rjust
时,您将在
浮点上应用
rjust
。另一种方法是右对齐以下位置的
:fill.precision
符号:

应用于
电路循环

print("The Circumference is : {:10.2f}".format(circleCircumference))
仅使用
%
可以对以下各项执行相同的操作:

>>> circleCircumference = 3.29920
>>> "Circle circumfence: %10.2f" % circleCircumference
'Circle circumfence:        3.30'

如果您只需要小数点后2位,您可以使用建议的相同答案,但稍作修改

print("The Circumference is : {:>10.2f}".format(circleCircumference))

这会告诉print语句将CircleCircumReference打印到2位作为浮点数,同时在10个字符的列中对其进行右对齐。

如果您只需要2位小数,可以使用建议的相同答案,但稍微修改一下

print("The Circumference is : {:>10.2f}".format(circleCircumference))
这会告诉print语句将circlecircumreference作为浮点打印到2个位置,同时在10个字符的列中对其进行右对齐