Question: Python: # write a program that prints out the average of # the negative numbers in the list that appear before a 999 is detected.
Python: # write a program that prints out the average of # the negative numbers in the list that appear before a 999 is detected. This is my solution, but is there a better way?
def main(): MyList = [ 23, -45, 6, -23, -9, 77, 54, -54, 21, -2, 8, -3, -23, 45, 93, -43, 999, -2, 3, 78, 90 ]
count_neg = 0 # count of negative numbers total_neg = 0 # sum or total of negative numbers
for num in MyList: if num < 0: # check for negative numbers count_neg += 1 total_neg = total_neg + num elif (num == 999): # stops when it finds 999 break # output print("Average negative number is : ", total_neg*1.0/count_neg) main() print("The end of processing")
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
