A Python program to get absolute sum of number strings

The code works for an input of three strings, each on a new line of stdin.

import sys
def isNumber(s):
for i in range(len(s)):
if s[i] == '-':
┆ ┆ try:
┆ ┆ ┆ if not s[i+1].isdigit():
┆ ┆ ┆ ┆ return False
┆ ┆ except Exception:
┆ ┆ ┆ return False
if ord(s[i]) >= 48 and ord(s[i]) <= 57:
┆ ┆ return True
return False
sum = 0
# It reads three input lines from stdin
# and finds cumulative abs sum of only numbers
for i in range(3):
line = sys.stdin.readline()
x = isNumber(line.strip())
print x
if x:
sum += abs(int(line.strip()))
print(sum)
view raw add_only_num.py hosted with ❤ by GitHub

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: