From a880e0a3ab113aab4e689b6849a9ad6a643ce208 Mon Sep 17 00:00:00 2001 From: ftm-2005 Date: Wed, 5 Jun 2024 12:23:21 +0300 Subject: [PATCH] Update calculator.py --- calculator.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/calculator.py b/calculator.py index d13630a..ad14830 100644 --- a/calculator.py +++ b/calculator.py @@ -31,17 +31,20 @@ '3' """ def add(s: str) -> str: - sum = 0 - if (s == ""): - return "0" - s = s.replace('\n', ',') - L = s.split(",") - for i in range(len(L)): - if "." in L[i]: - sum += float(L[i]) - else: - sum += int(L[i]) - return str(sum) + if (s[-1] == ',' or s[-1] == '\n'){ + return 'Number expected but EOF found.' + } + sum = 0 + if (s == ""): + return "0" + s = s.replace('\n', ',') + L = s.split(",") + for i in range(len(L)): + if "." in L[i]: + sum += float(L[i]) + else: + sum += int(L[i]) + return str(sum)