I'm trying to write a function that recieves any length string of positive or negative whole numbers and adds each number to the total, as long as the value doesn't go below zero. (It returns 0 for any invalid or empty input.)
I'm having trouble writing a loop that re-sets the count to zero when it becomes negative and continutes adding from where it left off.
e.g.
input: 1, 2, -4, 1, 1
output: 2
Here's my code:
def sum_earnings():
values = input("Enter a string of pos &/or neg numbers separated by commas (e.g. 1,-3,0,-4): ").split(',')
earnings = 0
try:
for i in values:
earnings += int(i)
while earnings >= 0:
earnings += int(i)
else:
earnings = 0
continue
print(earnings)
except ValueError:
print(0)
return