Skip to content

Instantly share code, notes, and snippets.

@oze4
Last active April 27, 2022 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oze4/639672b58b8c702687f9a1f919f8852e to your computer and use it in GitHub Desktop.
Save oze4/639672b58b8c702687f9a1f919f8852e to your computer and use it in GitHub Desktop.
def run():
i = input("enter 20 numbers separated by spaces: ")
iList = i.split(" ")
validate_input(iList, 20)
nums = list_str_to_list_int(iList)
return calculate_sum(nums)
def validate_input(strList, expectedTotal):
l = len(strList)
if l != expectedTotal:
raise Exception("expected {0} got {1}".format(expectedTotal, l))
def list_str_to_list_int(nl):
return list(map(int, nl))
def calculate_sum(numList):
sum = { "evenTotal": 0, "oddTotal": 0 }
for n in numList:
if is_even(n):
sum["evenTotal"] += n
else:
sum["oddTotal"] += n
return sum
def is_even(n):
return n % 2 == 0
results = run()
print("sum of even nums: %s" % results["evenTotal"])
print("sum of odd nums: %s" % results["oddTotal"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment