Question
So I'm making a function, collect_hw, that collects the grades of homeworks until you press -1, which it will then give you the average of
So I'm making a function, collect_hw, that collects the grades of homeworks until you press -1, which it will then give you the average of those homeworks. When I was checking to see if the function responded correctly to inputting an invalid number (anything not between 0 and 100, the function works perfectly. However, when I input valid numbers, and then press -1, it says the average of those homeworks is the last homework grade you submitted, which would obviously be incorrect. Would be great if someone could help me fix my code!
def collect_hw():
num_hw = 0 total_hw = 0 avg_hw = 0
hw = int(input("Collecting grade information! Homeworks first," "they are worth 60 percent of your grade. Enter a" " grade (or -1 to stop) ")) while True: scores_hw = int(input("Enter a grade (or -1 to stop) ")) if scores_hw 100: print("Has to be between 0-100") elif scores_hw == -1: print("Your homework average is", avg_hw) return avg_hw break else: num_hw = num_hw + 1 total_hw = total_hw + scores_hw avg_hw = (total_hwum_hw)
If my explanation was confusing, please see examples below:
def collect_hw(): MIT Function: collect_hw Parameters: none Returns: the average of the hw num_hw = 0 total_hw = 0 avg_hw = 0 hw = int(input("Collecting grade information! Homeworks first," "they are worth 60 percent of your grade. Enter a" " grade (or -1 to stop) ")) while True: scores_hw = int(input("Enter a grade (or -1 to stop) ")) if scores_hw 100: print("Has to be between 0-100") elif scores_hw == -1: print("Your homework average is", avg_hw) return avg_hw break else: num_hw = num_hw + 1 total_hw = total_hw + scores_hw avg_hw = (total_hwum_hw) Collecting grade information! Homeworks first, they are worth 60 percent of your grade. Enter a grade (or -1 to stop) 100000000 Enter a grade (or -1 to stop) -20000000 Has to be between 0-100 Enter a grade (or -1 to stop) 20 Enter a grade (or -1 to stop) 40 Enter a grade (or -1 to stop) -1 Your homework average is 30.0 Exams second, they are worth 40 percent of your grade. Enter a grade (or -1 to stop) Collecting grade information! Homeworks first, they are worth 60 percent of your grade. Enter a grade (or -1 to stop) 20 Enter a grade (or -1 to stop) 40 Enter a grade (or -1 to stop) -1 Your homework average is 40.0 Exams second, they are worth 40 percent of your grade. Enter a grade (or -1 to stop)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started