calculating student/class average with python - python

python 3.3.3
I am trying to write a program for class and I am lost. Here is what I need to do.
I need to calculate an average per student based on grades entered.
I need to calculate a class average.
if a student enters a grade of -1 input of grades stop.
need to print a message with each students grade.
the students grade should show a numeric grade and a letter grade.
the message will be based off of the students letter grade.
how do i collect and store students name and test grades.
so that i can output it all at once to where it will show the students name.
thier numeric average, a letter grade based off that average,
and a statement based off the letter grade they recieved?
heres the code i have so far:
def main():
another_student = 'y'
while another_student == 'y' or another_student == 'Y':
student_average()
print()
another_student = input('do you have another student to enter (y/n) ? ')
while another_student == 'n' or another_student == 'N':
student_average_list()
class_average()
break
def student_average():
total = 0.0
print()
student_name = input('what is the students name? ')
print()
print()
print(student_name)
print('-------------------')
number_of_tests = int(input('please enter the number of tests : '))
for test_num in range(number_of_tests):
print('test number', test_num + 1, end='')
score = float(input(': '))
total += score
student_average = total / number_of_tests
print ()
print(student_name,"'s average is : ",student_average, sep='')
def student_average_list():
print ('kahdjskh')
def class_average():
print ('alsjd')
main()

I think this is close to what you're basically looking for. It defines aStudentclass to make data storage and processing a little easier to manage.
class Student(object):
def __init__(self, name):
self.name, self.grades = name, []
def append_grade(self, grade):
self.grades.append(grade)
def average(self):
return sum(self.grades) / len(self.grades)
def letter_grade(self):
average = self.average()
for value, grade in (90, "A"), (80, "B"), (70, "C"), (60, "D"):
if average >= value:
return grade
else:
return "F"
def main():
print()
print('Collecting class student information')
a_class = [] # "class" by itself is a reserved word in Python, avoid using
while True:
print()
print('{} students in class so far'.format(len(a_class)))
another_student = input('Do you have another student to enter (y/n) ? ')
if another_student[0].lower() != 'y':
break
print()
student_name = input('What is the student\'s name? ')
a_class.append(Student(student_name))
print()
print('student :', student_name)
print('-------------------')
number_of_tests = int(input('Please enter the number of tests : '))
for test_num in range(1, number_of_tests+1):
print('test number {}'.format(test_num), end='')
score = float(input(' : '))
if score < 0: # stop early?
break
a_class[-1].append_grade(score) # append to last student added
print_report(a_class)
def print_report(a_class):
print()
print('Class Report')
print()
for student in sorted(a_class, key=lambda s: s.name):
print('student: {:20s} average test score: {:3.2f} grade: {}'.format(
student.name, student.average(), student.letter_grade()))
print()
print('The class average is {:.2f}'.format(class_average(a_class)))
def class_average(a_class):
return sum(student.average() for student in a_class) / len(a_class)
main()

You need to keep a list of marks for the whole class. student_average function is doing too many things. Maybe make a function get_student_marks that just returns the list of a student's marks. You'd need an average function to compute the average of a list, which you could use for both student average and class average. Good luck!

Related

GPA Calculator + failure testing

My code is only inputting one print command when there are two that need to be put out. I know this problem is simple but I need a new perspective
here is my code:
name = input("What is your name? \n")
h1 = ("Class Name")
h2 = ("Class Grade")
h3 = ("Credit Hours")
point = input("\nEnter your class name followed by your letter grade and hours (say Done to stop input):\n")
class_data = []
while point != "Done":
words = point.split(" ")
if len(words) == 1:
print("Error: No spaces in string. Try again.")
elif len(words) > 4:
print("Error: Too many spaces in input. Try again. ")
else:
try:
class_name = words[0]
grades = (words[1])
hrs = int(words[2])
print("Name of class:", class_name)
print("Grade:", grades)
print("Class Hours:", hrs)
class_data.append((class_name, grades, hrs,))
except ValueError:
print("Error: Space not followed by an integer.")
point = input("\nEnter your class name followed by your letter grade and hours (say Done to stop input):\n")
def gpa_calculator(grades):
points = 0
i = 0
grade_c = {"A":4,"A-":3.67,"B+":3.33,"B":3.0,"B-":2.67, "C+":2.33,"C":2.0,"C-":1.67,"D+":1.33,"D":1.0,"F":0}
if grades != class_data:
for grade in grades:
points += grade_c[item[1]]
gpa = points / len(class_data)
return gpa
else:
return None
print("Name: ", name)
print("-" * 66)
print("%-17s|%13s|%7s|" % (h1, h2, h3))
print("-" * 66)
for item in class_data:
print("%-17s|%13s|%12s|" % (item[0], item[1], item[2]))
print("-" * 66)
print('Your projected GPA is: ',(gpa_calculator(grades)))
print("-" * 66)
if item[0] == "Computer-Science" and item[1] == "D":
print ("failing CS")
if item[0] == "Programming" and item[1] == "D":
print ("failing programming")
what i need help with are the last four lines
output:
What is your name?
Nich
Enter your class name followed by your letter grade and hours (say Done to stop input):
Programming D 10
Name of class: Programming
Grade: D
Class Hours: 10
Enter your class name followed by your letter grade and hours (say Done to stop input):
Computer-Science D 10
Name of class: Computer-Science
Grade: D
Class Hours: 10
Enter your class name followed by your letter grade and hours (say Done to stop input):
Done
Name: Nich
------------------------------------------------------------------
Class Name |Class Grade|Credit Hours|
------------------------------------------------------------------
Programming | D| 10|
Computer-Science| D| 10|
------------------------------------------------------------------
Your projected GPA is: 0.5
------------------------------------------------------------------
failing CS
I've tried elif and true commands this is the closest I've been to solving this.
You need another loop, like the one you used to print the grade table.
for item in class_data:
if item[1] in ("D", "F"):
print(f"failing {item[0]}")

"FinalGrade is not defined" loop question

In my code, I'm trying to display the student with the highest grade in the class after calculating each grade received and their weight. For someone reason, I'm getting that the finalGrade is not defined. How would I display the student with the highest grade and their final grade as well?
def welcome():
print("Hello and Welcome to Top Students")
print("Our program lists your students and displays the highest of honors")
print("Let's get started\n")
def aveGrade():
quizGrade = int(input("What is the grade this student is receiving for their quiz?\n"))
partGrade = int(input("What is the grade this student is receiving for their prarticipation?\n"))
assignGrade = int(input("What is the grade this student is is receiving for their assignment?\n"))
finalGrade = ((quizGrade * .35) + (partGrade * .15) + (assignGrade * .50))
print("The final grade for this student is", finalGrade)
def main():
studentnames = ["Fred", "Daphne", "Velma", "Norville"]
for student in studentnames:
print("The current grade standing for", student, "are as follows:\n")
aveGrade()
print("The student with the highest final grade is", max(studentnames), "with a total of",max(finalGrade))
Variables only exist in scopes. In this case finalGrades is defined in the aveGrade() function and can only be accessed from within this function. Therefore your main function does not "know" about the finalGrade variable. You can declare finalGrade in the global scope and then asign the value to it in the function.
But the better solution would be to return the value from the function like this:
def aveGrade():
quizGrade = int(input("What is the grade this student is receiving for their quiz?\n"))
partGrade = int(input("What is the grade this student is receiving for their prarticipation?\n"))
assignGrade = int(input("What is the grade this student is is receiving for their assignment?\n"))
finalGrade = ((quizGrade * .35) + (partGrade * .15) + (assignGrade * .50))
print("The final grade for this student is", finalGrade)
return finalGrade #Changed
And then use it like this:
def main():
studentnames = ["Fred", "Daphne", "Velma", "Norville"]
for student in studentnames:
print("The current grade standing for", student, "are as follows:\n")
finalGrade = aveGrade() #Changed
print("The student with the highest final grade is", max(studentnames), "with a total of",max(finalGrade))
Addition to the comment:
Velma remains your highest student because your students and their final grade are not connected in any way. Currently you are just calculating and returning the final grade but the program does not know that it belongs to a student. max(studentnames) will always return Velma because in alphabetical order Velma is the highest value. Also max(finalGrade) won't do much because in each iteration of your for loop finalGrade is getting overwritten by the return value of aveGrade() and therefore isn't a list where a max value can be determined. One approach is to save the student and their final grade as a key value pair in a dictionairy
def main():
studentnames = ["Fred", "Daphne", "Velma", "Norville"]
studentGradesDic = {} #Added
for student in studentnames:
print("The current grade standing for", student, "are as follows:\n")
studentGradesDic[student] = aveGrade() #Changed
print("The student with the highest final grade is", list(studentGradesDic.keys())[list(studentGradesDic.values()).index(max(studentGradesDic.values()))], "with a total of",max(studentGradesDic.values())) #Changed

output combines scores from all students instead of just the one list after repeat

Where am I going wrong, the first part does exactly what I want. I put the first student in with scores it converts and averages but after repeat, it combines the lists and averages ofoutput all subsequent students.
def determine_grade(score):
if (score > 89):
return "A"
elif (score > 79):
return "B"
elif (score > 69):
return "C"
elif (score > 59):
return "D"
elif (score <= 59):
return "F"
def calc_average(test_scores):
total = 0
for i in range(len(test_scores)):
total = total + test_scores[i]
return total/int(len(test_scores))
def main():
repeat="yes"
test_scores = []
while repeat.lower() == "yes":
student = input("\nEnter student name: ")
for i in range(2):
score = round(float(input("Enter score: ")))
test_scores.append(score)
average_score = calc_average(test_scores)
print("\nStudent Name: ", student)
for i in range(len(test_scores)):
print("Score: ",test_scores[i], "Grade: ",determine_grade(test_scores[i]))
print("Average: ", average_score, "Student: ",student)
repeat = input("\nEnter anther student? yes or no: ")
main()
The thing that is wrong with this piece of code is that when you finished collecting info from one student the info is still left in the list, and was mixed up with another student's info
def main():
repeat="yes"
test_scores = []
while repeat.lower() == "yes":
test_scores.clear()
student = input("\nEnter student name: ")
for i in range(2):
score = round(float(input("Enter score: ")))
test_scores.append(score)
average_score = calc_average(test_scores)
print("\nStudent Name: ", student)
for i in range(len(test_scores)):
print("Score: ",test_scores[i], "Grade: ",determine_grade(test_scores[i]))
print("Average: ", average_score, "Student: ",student)
repeat = input("\nEnter another student? yes or no: ")
main()
You need to clear the list before collecting again.
After this line:
while repeat.lower() == "yes":
put:
print(test_scores)
Run the script and enter marks for a couple of students.
I think you will see why it is summing all the students marks in one go.
By the way, sum(test_scores) will add up the elements of test_scores for you. No need
for a loop. Also try out:
for score in test_scores:
print(score)
In general, when you are tempted to write
for i in range(len(things)):
followed by
things[i]
inside the loop, it is usually easier to write
for thing in things:
do_something_with(thing)
Your code will be shorter, more readable and more efficient.
Replace your main fucntion with this one:
def main():
repeat="yes"
while repeat.lower() == "yes":
test_scores = [] # Inside the while loop so it is new every time
student = input("\nEnter student name: ")
for i in range(2):
score = round(float(input("Enter score: ")))
test_scores.append(score)
average_score = calc_average(test_scores)
print("\nStudent Name: ", student)
for i in range(len(test_scores)):
print("Score: ",test_scores[i], "Grade: ",determine_grade(test_scores[i]))
print("Average: ", average_score, "Student: ",student)
repeat = input("\nEnter anther student? yes or no: ")

Creating a Grade Book with classes - Python

I'm working on a project/exercise where I need to use OOP in Python to create a Grade Book. I've been learning Python and working with 3.8.3 for about 6 weeks now, so I'm still fairly new. The grade book has a basic menu where you can add an assignment, quiz, and final exam grades. I must use a class with an empty list for both the quiz and assignment grades. I did a rough draft of the code after reading a bit on OOP, and managed to get the class to function without using attributes and only one method as below:
class GradeBook:
def main_function():
quiz_scores = []
assignment_scores = []
while True:
try:
quiz_grade = float(input('Please enter a grade for the quiz, or press Enter to stop adding grades: '))
quiz_scores.append(quiz_grade)
except:
break
while True:
try:
assignment_grade = float(input('Please enter a grade for the assignment, or press Enter to stop adding grades: '))
assignment_scores.append(assignment_grade)
except:
break
print (quiz_scores)
print (assignment_scores)
print ('time for totals and averages')
quiz_total = sum(quiz_scores)
assignment_total = sum(assignment_scores)
print ('quiz total ' + str(quiz_total))
print ('assign total ' + str(assignment_total))
if len(quiz_scores) > 0:
quizScoreAverage = sum(quiz_scores)// len(quiz_scores)
else:
quizScoreAverage = 0
if len(assignment_scores) > 0:
assignmentScoreAverage = sum(assignment_scores) // len(assignment_scores)
else:
assignmentScoreAverage = 0
print ('quiz average ' + str(quizScoreAverage))
print ('assign average ' + str(assignmentScoreAverage))
GradeBook.main_function()
Here is where I am running into my issues. I need to split the code up into several methods/functions, one for quiz scores, one for assignment scores, one that will store the final exam score and do nothing more, and one for getting the current grade/average. I've been searching and searching but have hit a wall. The code works up until I attempt to append the user's input to the list in the class. Again this is just a rough draft of the code as follows:
class GradeBook:
# Need this at attribute level for all instances to acccess as there will be an instance the pulls the list to calculate overall grade
assignment_scores = []
# quiz_scores = [] ### - This is the other list that will also be used for the grade
def assignGrade(self, score):
self.score = score
self.assignment_scores.append(score)
#####################################################
'''
This will be a duplicate of the above code but will use values to store quiz grades instead
def quizGrade(self, score):
self.score = score
self.quiz_scores.append(score)
'''
#####################################################
while True:
try:
assignment_grade = float(input('Please enter a grade for the assignment, or press Enter to stop adding grades: '))
# Program works just fine up until this point. My issue is here. Trying to feed the user input into the class instance
# to update the class list that is stored as an attribute. Instead of appending it seems to throw an error,
# because it doesn't continue the try loop for input and after the break when the list is printed, no values are shown
assignment_grade = GradeBook.assignGrade(assignment_grade) # <------- THIS IS THE PROBLEM CHILD OF MY CODING
except:
break
#####################################################
''' This block will be used to get input for the quiz grade to append to quiz scores list
while True:
try:
quiz_grade = float(input('Please enter a grade for the assignment, or press Enter to stop adding grades: '))
quiz_grade = GradeBook.quizGrade(quiz_grade) #not sure if this is right?
except:
break
'''
#####################################################
I guess I'm just not getting a good grasp on the whole idea of sending information from one instance to another. Any input is greatly appreciated. My plan is once it all gets figured out I just need to plug in the code to my final draft here:
class GradeBook:
# Initializes empty list to store quiz and assignment grades
quiz_scores = []
assignment_scores = []
#####################################################
def quizScore(self, score)
# lines of code to append user input to quiz list for reference in class
#####################################################
def assignScore(self, score)
# lines of code to append user input to assignment list for reference in class
#####################################################
def finalScore(self, score)
# line of code to store the final exam grade for reference in the class
#####################################################
def currentAverage(self)
if len(self.assignment_scores) > 0:
assignmentScoreAverage = sum(self.assignment_scores) // len(self.assignment_scores)
else:
assignmentScoreAverage = 0
if len(self.quiz_scores) > 0:
quizScoreAverage = sum(self.quiz_scores) // len(self.quiz_scores)
else:
quizScoreAverage = 0
currentGrade = (0.4 * self.final_grade) + (0.3 * quizScoreAverage) + (0.3 * assignmentScoreAverage)
return currentGrade
#####################################################
print('''
Grade Book
0: Exit
1: Enter assignment grade
2: Enter quiz grade
3: Enter final exam grade
4: Display current grade
''')
while True:
try:
selection = int(input('Please enter a choice: '))
if selection == 0:
quit
elif selection == 1:
while True:
try:
assignment_grade = float(input('Please enter a grade for the assignment, or press Enter to stop adding grades: '))
GradeBook.assignScore(assignment_grade)
except:
break
elif selection == 2:
while True:
try:
quiz_grade = float(input('Please enter a grade for the assignment, or press Enter to stop adding grades: '))
GradeBook.quizScore(quiz_grade)
except:
break
elif selection == 3:
while True:
try:
final_grade = float(input('Please enter a grade for the assignment, or press Enter to stop adding grades: '))
GradeBook.finalScore(final_grade)
except:
break
elif selection == 3:
final_grade = float(input('Please enter a grade for the final exam: '))
if isdigit(final_grade)
GradeBook.finalScore(final_grade)
else:
print('Please check your input and try again.')
elif selection == 4:
print(GradeBook.currentAverage())
else:
print('Please check your input and try again.')
continue
(Please, use snake case for all your code, including methods. Camel case should only be used for classes) In python I would recommend creating a module(file) without class for your main function. You are using the assignGrade method as a class method. Actually, you never even created an instance of your class! To create an instance, you should call the class as follows:
my_grades = GradeBook()
Now, my_grades is "a variable"(an instance) that contains all the properties(attributes) you defined for your class. You can access them as follows:
my_grades.assignment_scores
Your loop will fail because you have not passed the self argument to the function you pointed as the problem. In python, all instance methods start with the self argument, but it will be the variable(instance) before the dot. So, you should call it from an instance of your class and not from the class itself. Using the one I created from the first example, you can change the line to:
my_grades.assignGrade(assignment_grade)
That should do.
Instead of creating and calling a main_function, in the module add the following:
if __name__ == '__main__':
... # Create your instances and manipulate them here
So following the guidelines that were provided to us, as well as the example inputs, I have the program working as intended. Since this was just an introduction class, we didn't get into using methods in separate files, so I did not use:
if __name__ == '__main__':
However, I would like any comments on it like readability and other coding practices. Working code below:
# Programmed by thelastesquire92
# Programming Assignment 7 - Grade Book App
# SEC290.B2.32789
'''
Program that focuses on Object Oriented Programming.
The user is presented with simple text menu, and following
input prompts they are allowed to enter grades for quizzes,
assignments, and a final exam. The program then calculates
the average grade based off of these inputs.
'''
class GradeBook:
# Initializes empty list for quiz and assignment grades, sets default final score to 0
quiz_scores = []
assignment_scores = []
final_exam_score = 0
#####################################################
# Adds grade input to list of quiz scores
def quiz_score(self, score):
self.quiz_scores.append(score)
#####################################################
# Adds grade input to list of assignment scores
def assignment_score(self, score):
self.assignment_scores.append(score)
#####################################################
# Updates value of final exam score
def final_score(self, score):
GradeBook.final_exam_score = score
#####################################################
# Calculates current grade average
def current_average(self):
if len(GradeBook.assignment_scores) > 0:
assignment_score_average = sum(GradeBook.assignment_scores)\
// len(GradeBook.assignment_scores)
else:
assignment_score_average = 0
if len(GradeBook.quiz_scores) > 0:
quiz_score_average = sum(GradeBook.quiz_scores)\
// len(GradeBook.quiz_scores)
else:
quiz_score_average = 0
current_grade = (0.4 * GradeBook.final_exam_score)\
+ (0.3 * quiz_score_average)\
+ (0.3 * assignment_score_average)
return current_grade
#####################################################
# Prints out the menu for user
menu = ('''
Grade Book
0: Exit
1: Enter assignment grade
2: Enter quiz grade
3: Enter final exam grade
4: Display current grade
''')
print(menu)
#####################################################
# Main body of program that loops until user selects option to exit
while True:
# Creates instance of GradeBook
my_grades = GradeBook()
try:
selection = int(input('\nPlease enter a choice: '))
#####################################################
# Option 0 that exits the program
if selection == 0:
break
#####################################################
# Option 1 that allows input for assignment grades
elif selection == 1:
while True:
try:
assignment_grade = float(input('\nPlease enter a grade for the assignment: '))
my_grades.assignment_score(assignment_grade)
break
except:
print('\nPlease check your input and try again.')
continue
#####################################################
# Option 2 that allows input for quiz grades
elif selection == 2:
while True:
try:
quiz_grade = float(input('\nPlease enter a grade for the quiz: '))
my_grades.quiz_score(quiz_grade)
break
except:
print('\nPlease check your input and try again.')
continue
#####################################################
# Option 3 that allows input for final exam grade
elif selection == 3:
while True:
try:
final_grade = float(input('\nPlease enter a grade for the final exam: '))
my_grades.final_score(final_grade)
break
except:
print('\nPlease check your input and try again.')
continue
#####################################################
# Option 4 that displays current grade average
elif selection == 4:
average = my_grades.current_average()
print('\nYour current grade average is: ' + str(average))
else:
print('\nPlease check your input and try again.')
continue
except:
print('\nPlease check your input and try again.')
print(menu)
continue

Python - Problems with my loop and print out format

I am trying to let a user keep entering module and grades and store it as a dictionary {module:grades} and store this dictionary in a list.
I have 3 issues in this.
I am unable to use F6 in the second raw_input which is F7.
Secondly, I am using 'done' to stop the loop. When I print the information it looks like this:
{done : 100}
{done : 80}
and so on.. So the module name keeps getting replaced by the word 'done'.
And lastly I am trying to make the print out appear as follows: (which is not hapening now..)
Grades: Computer Science: 100
Computer Graphics: 80
I have finished the rest of my work less this portion which I am stuck with. Any assistance is deeply appreciated. Thank you so much.
students = []
class Student:
grades = {}
def setGrades(self, grades):
self.grades = grades
def addStudent():
while F6 != 'done':
F6 = raw_input("Please enter module name. type 'done' to quit: ")
if F6 == 'done':
break
F7 = raw_input("Please enter the grades for " ,F6, ':')
student.setGrades({F6:F7})
For starters, your code (as is) should look something like this
students = []
class Student:
grades = {}
def setGrades(self, grades):
self.grades = grades
def addStudent():
while True:
F6 = raw_input("Please enter module name. type 'done' to quit: ")
if F6 == 'done':
break
F7 = raw_input("Please enter the grades for " ,F6, ':')
student.setGrades({F6:F7})
In my opinion you should also have a better api for setting Grades. Something like this would suffice imo:
def set_grades(self, lesson, grade):
self.grades[lesson] = grade
Finally in order to print the grades you should have a method like this:
def print_grades(self):
for lesson, grade in grades.items():
print lesson, grade
Last but not least in your raw input, in order to use F6 you have to do something like that:
F7 = raw_input("Please enter the grades for %s: " % F6)
To sum it all up, if I were you my code would look something like this:
class Student:
grades = {}
def set_grades(self, lesson, grade):
self.grades[lesson] = grade
def addStudent():
while True:
F6 = raw_input("Please enter module name. type 'done' to quit: ")
if F6 == 'done':
break
F7 = raw_input("Please enter the grades for %s: " % F6)
student.setGrades(F6, F7)
def print_grades(self):
for lesson, grade in grades.items():
print lesson, grade

Categories