[英]How to round to 4 decimal places?
我有以下代码,我想在 Python 3 中按小数点后 4 位四舍五入。我尝试使用 round,但一直出现语法错误。
fname = input("Enter a file name: ")
try:
ffile = open(fname)
ssum = 0
nline = 0
for nline, line in enumerate(ffile):
if line.strip().startswith("X-DSPAM-Confidence:"):
colon = line.find(":")
slash = line.find("\\")
current_av = line[colon+1:slash]
ssum = ssum + float(current_av)
print ("Average Spam confidence: ", ssum/nline)
except:
print "This file was not found"
我试过ssum = (round(ssum, 4))
,但小数点没有四舍五入。
我试过 ssum = (round(ssum, 4)),但小数点没有四舍五入。
是的,这是正确的圆角方法。 您在外部有一组额外的括号,但这不会影响调用。 你在做什么就像这个例子:
>>> import math
>>> round(math.pi, 4)
3.1416
一直收到语法错误。
print "This file was not found"
那行不通,在python3中不行。 调用打印函数时使用括号:
print("This file was not found")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.