From 2d4ab91f44336c48b404d18d0bdb38365bed8937 Mon Sep 17 00:00:00 2001 From: Kushagra Gupta Date: Fri, 5 Oct 2018 13:27:32 +0530 Subject: [PATCH] Made it into a function. --- beginner/sum-earnings_Kushagra-0801.py | 35 +++++++++++++++----------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/beginner/sum-earnings_Kushagra-0801.py b/beginner/sum-earnings_Kushagra-0801.py index 1eab79c..7c661fd 100644 --- a/beginner/sum-earnings_Kushagra-0801.py +++ b/beginner/sum-earnings_Kushagra-0801.py @@ -1,16 +1,21 @@ -"""Solution for Issue #8 (Sum Eanings Challenge).""" +"""Solution for Issue #8 (Sum Earnings Challenge).""" -try: - input_ = input() - required_nums = input_.count(',') + 1 - history = list(map(int, input_.split(','))) - if len(history) != required_nums: - raise ValueError - balance = 0 - for i in history: - balance += i - if balance < 0: - balance = 0 - print(balance) -except ValueError: - print('0') + +def sum_earnings(finance_data): + """Validate and process input.""" + try: + required_nums = finance_data.count(',') + 1 + history = list(map(int, finance_data.split(','))) + if len(history) != required_nums: + raise ValueError + balance = 0 + for i in history: + balance += i + if balance < 0: + balance = 0 + return balance + except ValueError: + return 0 + + +print(sum_earnings(input()))