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


Messages In This Thread
Calcolate the average of numbers from a .txt file - by francescomiles - Mar-27-2021, 10:24 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Read strings and numbers in columns from a file suvadip 4 3,076 Aug-11-2020, 09:37 PM
Last Post: suvadip
  Removing Certain Numbers From File chascp 2 2,177 Feb-07-2020, 04:04 PM
Last Post: chascp
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,887 May-09-2019, 12:19 PM
Last Post: Pleiades
  writing numbers to csv file SchroedingersLion 7 4,217 Dec-20-2018, 10:48 AM
Last Post: perfringo
  Help deleting numbers from file names Shane 5 4,985 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,717 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