Home > Software engineering >  I'm trying to convert this string of numbers to integer and read from a text file
I'm trying to convert this string of numbers to integer and read from a text file

Time:11-09

import statistics
def main():
 with open('Grades.txt', mode='w') as Grades:
    Grade = input("Please enter student grades. " ) #Gets Input from User 
    Grades.write(str(Grade)   '\n') #Has the student grades written into the Grades text file and has each of them on a new line.
 



with open('Grades.txt', mode='r') as Grades: #Opens the Grades File in read and then prints Mean, Total, Count, Median, Min, Max, Std, Grades
    print(f'{"Mean"}')
    for record in Grades:
       grade = record.split()
       print("Mean of the sample is  " %(statistics.mean('grade')))
    print(f'{"Total"}')
    print(f'{"Count"}')
    print(f'{"Median"}')
    for record in Grades:
       grade = record.split()
       print("Median of the sample is  " %(statistics.median('grade')))
    print(f'{"Min"}')
    for record in Grades:
       grade = record.split()
       print("Minimum of the sample is  " %(min('grade')))
    print(f'{"Max"}')
    for record in Grades:
        grade = record.split()
        print("Maximum of the sample is  " %(max('grade')))
    print(f'{"Std"}')
    for record in Grades:
        grade = record.split()
        print("Standard Deviation of the sample is % s " %(statistics.mean('grade')))
    
    for record in Grades: #For the record in Grades Files It takes the grade in the record and splits and prints the Grades
        grade = record.split()
        print(f'This is the Grades of the students {grade}')
        
        
main()

I'm stuck on this still learning python. Trying convert the str to int and then get the mean, median, total etc ...................................................................................................

CodePudding user response:

I don't usually feel good about doing homework for others, but in this case I think you did give it a fair try.

Here's how your code should look. I have eliminated the write/read from file, and just split the user's input directly. I store the numbers in a single list, and then do the statistics on that list.

The purists will note that the initial conversion loop could be done with a list comprehension. That just confuses the issue here.

import statistics
def main():
    gradeinput = input("Please enter student grades. " )
    grades = []
    for grade in gradeinput.split():
        grades.append( int(grade) )

    print('Mean')
    print("Mean of the sample is ", statistics.mean(grades))
    print('Total')
    print("Total of the sample is ", sum(grades))
    print('Count')
    print("Length of the sample is ", len(grades))
    print('Median')
    print("Median of the sample is ", statistics.median(grades))
    print('Min')
    print("Minimum of the sample is ", min(grades))
    print('Max')
    print("Maximum of the sample is ", max(grades))
    print('Std')
    print("Standard Deviation of the sample is ", statistics.mean(grades))
    
    print('This is the Grades of the students:')
    print(grades)
        
main()

CodePudding user response:

Hey @RustyShackleford,

import statistics

def main():
    with open("Grades.txt") as Grades:
         grade_list = list(map(int, Grades.read().split("\n")))
         print('Grades of the students:')
         print(grade_list)
         print("Mean of the grades is ", statistics.mean(grade_list))
         print("Total of the grades is ", sum(grade_list))
         print("Length of the grades is ", len(grade_list))
         print("Median of the grades is ", statistics.median(grade_list))
         print("Minimum of the grades is ", min(grade_list))
         print("Maximum of the grades is ", max(grade_list))
         print("Standard Deviation of the grades is ", statistics.stdev(grade_list))

main()

In python to convert string to integer we can simply do-:

int("56")

It would give you 56 and you can perform mathematical operation on top of this.

But if you try this -:

int("23fdd")

it would give you an error and 23fdd is not number.

  • Related