用户希望在Python中手动设置小数点



我试着制作一个程序来计算用户可以自己设置小数点的圆形面积。我该怎么办?

r = int(input("Radient = ")
y = int(input("How many point you want to show in out put = "))
a = 2*pi*r
print("Circular Area is %.yf" %(a))

您可以使用{}.format(...)而不是%

示例:

print("{0:.{1}f}".format(3.14159, 4))

如果您使用的是python 3.6或更高版本,您可以使用当前的结构来尝试类似的操作:

r = int(input("Radient = "))
y = int(input("How many point you want to show in out put = "))
a = 2*pi*r
print(f"Circular Area is %.{y}f" %(a))

你可以在这里阅读更多关于f-string的

最新更新