logo CodeStepByStep logo

negative_sum

Language/Type: Python file input

Write a function named negative_sum that accepts a string as a parameter representing a file name, which reads input from that file containing a series of integers, and determine whether the sum starting from the first number is ever negative. The function should pra message indicating whether a negative sum is possible and should return True if a negative sum can be reached and False if not. For example, if the file contains the following text, your function will consider the sum of just one number (38), the sum of the first two numbers (38 + 4), the sum of the first three numbers (38 + 4 + 19), and so on up to the sum of all of the numbers:

38 4 19 -27 -15 -3 4 19 38

None of these sums is negative, so the function would produce the following message and return False:

no negative sum

If the file instead contains the following numbers, the function finds that a negative sum of -8 is reached after adding 6 numbers together (14 + 7 + -10 + 9 + -18 + -10):

14 7 -10 9 -18 -10 17 42 98

It should output the following and return True, indicating that a negative sum can be reached:

-8 after 6 steps
Function: Write a Python function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.