Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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!

image text in transcribed

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:

image text in transcribed

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Object Oriented Databases Prentice Hall International Series In Computer Science

Authors: John G. Hughes

1st Edition

0136298745, 978-0136298748

More Books

Students also viewed these Databases questions

Question

L A -r- P[N]

Answered: 1 week ago