Question

in python im trying to get this to print to the file i it created def...

in python

im trying to get this to print to the file i it created


def calculateTotal():
filename = input("Enter a name for the file : ")#ask users for a name for file
no_of_students = int(input("Enter the number of students in class : "))
data = []
with open(filename,"w+") as f:
for index in range(no_of_students):
grades = input("Enter Name of student plus grade :").split()

# store the student data in array "data"
data.append(grades)
# store the total grades in "total"
total = 0

for i in data:
total += int(i[1])

# store the average grades in average
average = total / no_of_students
file = open(filename)
f.write("Number of students\ttotal_grades Average\n")
f.write("\t"+str(number_students)+"\t\t"+str(score)+"\t "+str(averageScore))
f.write("Average score of students: %.2f.\n" % averageScore)
print("Number of students \ total_grades\n")
f.close()
print("Total grades : " + str(total))
print("Average grade : " + str(average))


calculateTotal()

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

def calculateTotal():

    filename = input("Enter a name for the file : ")#ask users for a name for file

    no_of_students = int(input("Enter the number of students in class : "))

    data = []

    with open(filename,"w+") as f:

        for index in range(no_of_students):

            grades = input("Enter Name of student plus grade :").split()

        # store the student data in array "data"

        data.append(grades)

        # store the total grades in "total"

        total = 0

        for i in data:

            total += int(i[1])

        # store the average grades in average

        averageScore = total / no_of_students

        f.write("Number of students\ttotal_grades Average\n")

        f.write("\t"+str(no_of_students)+"\t\t"+str(total)+"\t "+str(averageScore)+"\n")

        f.write("Average score of students: %.2f.\n" % averageScore)

    print("Number of students \t"+str(no_of_students)+"\n")

    print("Total grades : " + str(total))

    print("Average grade : " + str(averageScore))

calculateTotal()

Add a comment
Know the answer?
Add Answer to:
in python im trying to get this to print to the file i it created def...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a Word doc file (name of the file: numbers) using Python. Write the following numbers...

    Create a Word doc file (name of the file: numbers) using Python. Write the following numbers to the Word document as shown below and read these numbers from the file and calculate and print the sum and average of these numbers. 9 5 7 6 3 After that, read these values from the file and calculate the sum and average of these numbers.   I have wrote this so far, but doesnt work! def numbersfile(): f = open("numbers.docx, "w") for i...

  • Python : Need help with output of table Code: # Number of students print('How many students...

    Python : Need help with output of table Code: # Number of students print('How many students are in the class?') student_Number = int(input()) # Store list of students who are valid student_List = [] for i in range(0, student_Number): # To store current student data student_Data = [] print('Enter the name of student:') # Name of Students student_Name = input() if len(student_Name)> 24: print('Name of student cannot exceed 24 characters.') continue print('Enter the gpa of student:') # GPA of Student...

  • I am having trouble with my Python code in this dictionary and pickle program. Here is...

    I am having trouble with my Python code in this dictionary and pickle program. Here is my code but it is not running as i am receiving synthax error on the second line. class Student: def__init__(self,id,name,midterm,final): self.id=id self.name=name self.midterm=midterm self.final=final def calculate_grade(self): self.avg=(self.midterm+self.final)/2 if self.avg>=60 and self.avg<=80: self.g='A' elif self.avg>80 and self.avg<=100: self.g='A+' elif self.avg<60 and self.avg>=40: self.g='B' else: self.g='C' def getdata(self): return self.id,self.name.self.midterm,self.final,self.g CIT101 = {} CIT101["123"] = Student("123", "smith, john", 78, 86) CIT101["124"] = Student("124", "tom, alter", 50,...

  • PYTHON 3 Object Oriented Programming ***a9q3.py file below*** class GradeItem(object): # A Grade Item is anything...

    PYTHON 3 Object Oriented Programming ***a9q3.py file below*** class GradeItem(object): # A Grade Item is anything a course uses in a grading scheme, # like a test or an assignment. It has a score, which is assessed by # an instructor, and a maximum value, set by the instructor, and a weight, # which defines how much the item counts towards a final grade. def __init__(self, weight, scored=None, out_of=None): """ Purpose: Initialize the GradeItem object. Preconditions: :param weight: the weight...

  • I need help writing python code with following instructions. You will write a program that reads...

    I need help writing python code with following instructions. You will write a program that reads a data file. The data file contains ticket IDs and ticket prices. Your job is to read these tickets (and prices). find minimum, maximum and average ticket prices and output a report file. The report file should look exactly (or better than) the one attached (output.txt). Input: A31 149.99 B31 49.99 A41 179.99 F31 169.99 A35 179.99 A44 169.99 open "input.txt" file using open()...

  • Your assignment is to write a grade book for a teacher. The teacher has a text file, which includ...

    Your assignment is to write a grade book for a teacher. The teacher has a text file, which includes student's names, and students test grades. There are four test scores for each student. Here is an example of such a file: Count: 5 Sally 78.0 84.0 79.0 86.0 Rachel 68.0 76.0 87.0 76.0 Melba 87.0 78.0 98.0 88.0 Grace 76.0 67.0 89.0 0.0 Lisa 68.0 76.0 65.0 87.0 The first line of the file will indicate the number of students...

  • C++ Write a function parseScores which takes a single input argument, a file name, as a string. Your function should read each line from the given filename, parse and process the data, and print the r...

    C++ Write a function parseScores which takes a single input argument, a file name, as a string. Your function should read each line from the given filename, parse and process the data, and print the required information. Your function should return the number of student entries read from the file. Empty lines do not count as entries, and should be ignored. If the input file cannot be opened, return -1 and do not print anything.     Your function should be named...

  • Write a function that takes, as an argument, the name of a file, fileName . Your...

    Write a function that takes, as an argument, the name of a file, fileName . Your program should open (and read through) the file specified, and return the maximum value in the file. Name this function maxValueInFile(fileName). def openFile(): # Prompt for the file name filename =input("Enter a file name: " ) # Open the file for reading file = open(filename) # Prompt for the target value target =input("Enter a threshold value: ") # Initialize the counter counter = 0...

  • Use the file processing example program shown at the bottom. Modify the program to enter the...

    Use the file processing example program shown at the bottom. Modify the program to enter the grades, and count number of grades. Also get total and average grade. Use the following data for grades: 79, 99, 85, 97, 88, 95, 100, 87, 94 EXAMPLE OUTPUT Total: 9 grades Total grade sum: 824 Average grade: 92 Letter grade: A / Using Break, and Continue #include "stdafx.h" #include using namespace std; int main() { int i = 0; for (int x =...

  • Write a complete C++ program that reads students names and their test scores from an input...

    Write a complete C++ program that reads students names and their test scores from an input text file. The program should output each student’s name followed by the test scores and the relevant grade in an output text file. It should also find and display on screen the highest/lowest test score and the name of the students having the highest/lowest test score, average and variance of all test scores. Student data obtained from the input text file should be stored...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT