so, my question is, how to convert a string number to a float while gewtting it from a text file (i am using the open method to access and write, modify or read the content of a plain text file that i've created before). this is the question:
The final grades for Programming Methods course unit are recorded in a file final.txt. Write a Python program that reads the grades from the file final.txt and analyzes grades by determining the average grade, the number of grades above the average and the modal grade.
so, i'm asked to create a text file and read the grades from that file and do the above task (in the question) with that file. BUT the problem is that, when i try to access and use the grades from that text file (which are strings by default) they tell be that i cannot convert those grades into float in order to use them. there is the error ValueError: could not convert string to float: '\n'
this is the program:
def file_average(filename):
file=open(filename)
number_list=file.readline()
total=0
for number in number_list:
total=total+float(number)
file.close()
return total/len(number_list)
input_filename=input("file: ")
average=file_average(input_filename)
print(f"average is equal to: {average}")
note: i've alredy created a text file called "final.txt" and it is stored in the same directory (folder) as the python file with the program inside.
i tried to access the text file and use the grades (numbers inside which are string by default). but when am trying to call them to use them in the list am creating, this error comes out :
ValueError: could not convert string to float: '\n'
so, how can i do to correct that conversion problem ?