I need help finishing a grade calculator program in python
For my computer science class I have to make a program that will calculate grades. This is my first computer science class (no prior experience) so I am struggling through it.
These are the directions:
Ask the user for the number of tests, assignments, quizzes, and labs in their course.
Ask the user if there is a final with a separate weight from the tests above, e.g. a course has 2 tests, each weighing 12.5%, and 1 final weighing 15%.
For each category having a number > 0
a. Prompt the user for the weighted percent, out of 100%, which should total 100% for all categories!!!
b. Get the score(s) for the category.
c. If the category is labs, then sum all the scores.
d. Else, average the scores.
e. Calculate the weighted average for the category.
Using the weighted average of each category, calculate the grade in the course.
Ask the user if he/she wants to calculate a grade for another class.
If the user responds yes, then go back to step 1.
Else, end the program.
My code so far:
def main(): lists = get_user_input() get_scores(lists); get_weighted_average(lists) def get_user_input(): # How many? t = int(input("How many tests?: ")) a = int(input("How many assignments?: ")) q = int(input("How many quizzes?: ")) l = int(input("How many labs?: ")) # How much weight on grade? tw = float(input("Enter weight of tests: ")) aw = float(input("Enter weight of assignments: ")) qw = float(input("Enter weight of quizzes: ")) lw = float(input("Enter weight of labs: ")) lists = [t, a, q, l, tw, aw, qw, lw] return lists def get_scores(lists): # What are the scores? scores =  * 5 for(x in range(lists): test_scores = float(input("enter your test scores: ")) scores[x] = test_scores for(x in range(lists): test_scores = float(input("enter your assignment scores: ")) scores[x] = assignment_scores for(x in range(lists): test_scores = float(input("enter your quiz scores: ")) scores[x] = quiz_scores for(x in range(lists): test_scores = float(input("enter your lab scores: ")) scores[x] = lab_scores sumlabs = 0 for(x in range(lists): sumlabs = sumlabs + scores[x] print(sumlabs) def get_weighted_average(lists): main()
I am not sure how to proceed so any help is much appreciated.
Averaging a score means adding them up and dividing by the number of them. You can use the fact that a list has a way to tell you how many elements it has. For example if x is a list then len(x) is the number of things in the list. You should be careful about not trying to calculate the score of an empty list, because that would mean dividing by zero. In the following function the 'if score_list:' will be true if there is something in the list. Otherwise it returns None (since the average is not defined).
def average_score(score_list): if score_list: # makes sure list is not empty total = 0 # our total starts off as zero for score in score_list: # starts a loop over each score total += score # increases the total by the score return total / len(score_list) # returns aveage
To form a list of scores from user input, presuming the user will put them all on the same line when you prompt them, you can get a string from the user (presumably like: "13.4 12.9 13.2" And then use the string's split method to convert this to a list of strings like ["13.4", "12.9", "13.2"], and then convert this list of strings to a list of floats. Finally using the average function above you can get an average:
test_score_entry = raw_input("Enter your test scores separated by spaces: ") test_sore_strings = test_score_entry.split() test_scores = [float(_) for _ in test_score_strings] average_score = average(test_scores)