反復処理可能ではない私は、割り当てを完了するために以下のコードを書いた:のPython:Floatオブジェクトが
fname = raw_input("Enter file name: ")
fh = open(fname)
total = 0
count = 0
for line in fh:
if not line.startswith("X-DSPAM-Confidence:") : continue
pos = line.find(':')
num = float(line[pos+1:])
for number in num:
total = total +num
count += 1
print 'Average spam confidence:', total/count
システムは
反復可能ではないことを読んで、エラーメッセージが出てきますフロートオブジェクトが
私は間違いを知っていましたfor number in num:
正しい答えは
fname = raw_input("Enter file name: ")
fh = open(fname)
total = 0
count = 0
for line in fh:
if not line.startswith("X-DSPAM-Confidence:") : continue
pos = line.find(':')
num = float(line[pos+1:])
total = total +num
count += 1
print 'Average spam confidence:', total/count
私の質問は:正解では 、浮動オブジェクトもiterableですか? 助けてくれてありがとう!
を呼び出すことによって
total
に直接num
を追加するだけです。 –