Mar-05-2020, 11:14 AM
(Mar-05-2020, 01:54 AM)Jagsrs28 Wrote: Getting the average credits and average study hours, I assume I would use a for loop.I didn't understand: why do you need to use a for-loop. You have the number of students and total value of credits. Mean value is a fraction of the total amount of credits and the number of students, isn't it?
I would also suggest some refactorings, e.g.
# define a dict grade_mapper = {'a': 15, 'b': 12, 'c': 9, etc... } # you can rewrite if/elif-series as follows: study_hours = grade_mapper.get(desiredGrade.lower(), None) # I leave camel-case just for clarity if study_hours is None: print("Invalid grade")Also, you can convert variables to lowercase in if-conditions, e.g.
Instead of coding
if some_var == 'a' or some_var == 'A': ...you can do:
if some_var.lower() == 'a': ...