Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python problem: How do you include the number that you are trying to process if it's the sentinel? This is my solution won't include -999

Python problem:

How do you include the number that you are trying to process if it's the sentinel? This is my solution won't include -999 in the process, but I am not sure how do this...

# write a program that prints out the average of # the negative numbers in the list that appear before a -999 is detected.

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

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

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions