-3
「」から読み込まれ、その後、あなたは書き込みのためにファイルを開いているは、ファイル内の値を入力環境により、平均値を計算すると、コードが
num = 3
try: #set an exception in case of a file Error
while num >=0: '''read in values and place them in a file'''
value = int(input("Enter values: "))
my_file = open('my_data.txt', 'w+')
my_file.write(str(value))
numbers = my_file.readlines()
num -=1
my_file.close()
except IOError:
print('FILE FAILURE')
'''iterate to find the sum of the values in a file'''
total = 0
for ln in numbers:
total += int(ln)
'''Calculate the average'''
avg = total/len(numbers)
print("The average is %d"%(avg))#FIXME: does not calculate average
"コードが間違った出力を生成する"どのくらい正確ですか?入力と出力の例を提供する必要があります。それを切り捨てるすべての反復でファイルを開いていることに注意してください。なぜあなたはファイルに書き込んですぐにそれを読むのですか? – DeepSpace
@DeepSpace値を入力すると、たとえば、2,4,7,5となります。出力は常に3番目の数値です。この例では7.ありがとう! – Umubale
私のコメントの残りの部分も参照してください。 – DeepSpace