""" Write a Python program to calculate the sum over a container. """ """ SOLUTION: """ import sys from pathlib import Path from math import sqrt abspath = Path(__file__) toolspath = abspath.parents[3] sys.path.append(str(toolspath / 'tools')) from GetFromUser import GetListOfIntegers myList = GetListOfIntegers( ) #This is the container (list). See the implementation of GetListOfIntegers for details print(sum(myList))
Sample data : 3, 5, 7, 23 Output : List : ['3', ' 5', ' 7', ' 23'] Tuple : ('3', ' 5', ' 7', ' 23') """ """ SOLUTION: """ import sys from pathlib import Path abspath = Path(__file__) toolspath = abspath.parents[3] sys.path.append(str(toolspath / 'tools')) from GetFromUser import GetListOfIntegers while True: myList = GetListOfIntegers() if (len(myList) <= 1): print("You should give at least two numbers. Try again.") continue else: break numbersTuple = tuple(myList) print('List: %s' % (myList)) print('Tuple: %s' % (numbersTuple, ))