Python Forum
Calcolate the average of numbers from a .txt file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calcolate the average of numbers from a .txt file
#1
I start to learn programming few weeks ago and I'm trying to do an exercise that ask me to calculate the average of a list of number from a text file. This is my code:
file=input('Enter a file name: ')
open=open(file)
count=0
for line in open:
    line=line.rstrip()
    if line.startswith('X-DSPAM-Confidence:'):
        count=count+1
        search=line.find(':')
        numbers=line[search+1:]
        float(numbers)
        Media=sum(numbers)/count
        print('Media spam confidence: ', Media)
It gives me back this error:
Error:
Traceback (most recent call last): File "C:\Users\frank\OneDrive\Documenti\Atom\Esercizi\Esercizio_file.py", line 11, in <module> Media=sum(numbers)/count TypeError: unsupported operand type(s) for +: 'int' and 'str'
I understand the error but I don't know how to fix it
snippsat write Mar-27-2021, 10:26 AM:
Added code tag in your post,look at BBCode on how to use.
Reply
#2
On line 10 float(numbers) dos nothing as it not saved in a variable.
Then will make this error.
>>> n = '0.4855'
>>> count = 5
>>> sum(n) / 5
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'

# But dos not help is save variable as will get new error
>>> n = float(n)
>>> sum(n) / 5
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
TypeError: 'float' object is not iterable
 
So untested if you parse numbers correct could make another variable total at top.
Then calculate average when loop is finish.
file = input('Enter a file name: ')
f_open = open(file) # open is used bye Python
count = 0
total = 0
for line in f_open:
    line = line.rstrip()
    if line.startswith('X-DSPAM-Confidence:'):
        search = line.find(':')
        numbers = line[search+1:]
        count = count + 1
        total = total + float(numbers)

average = total / count
print(f"Average confidence: {average}")
Reply
#3
Thank you so much
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Read strings and numbers in columns from a file suvadip 4 2,784 Aug-11-2020, 09:37 PM
Last Post: suvadip
  Removing Certain Numbers From File chascp 2 2,053 Feb-07-2020, 04:04 PM
Last Post: chascp
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,668 May-09-2019, 12:19 PM
Last Post: Pleiades
  writing numbers to csv file SchroedingersLion 7 4,001 Dec-20-2018, 10:48 AM
Last Post: perfringo
  Help deleting numbers from file names Shane 5 4,796 Feb-04-2018, 10:42 PM
Last Post: wavic
  Script to average 2 columns in .csv file, and print results to the file name. Manveer_Dhillon 3 3,556 Sep-18-2017, 10:27 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020