Home > Software design >  Print Sum of Array
Print Sum of Array

Time:01-12

import math

import os

import random

import re

import sys


def simpleArraySum(ar_count,ar):
    if ar_count == len(ar):
         print(sum(ar))
    else:
        print("Invalid Input")
    


if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    ar_count = int(input().strip())

    ar = list(map(int,input().rstrip().split()))

    result = simpleArraySum(ar_count,ar)

    fptr.write(str(result)   '\n')

    fptr.close()

I am trying to print sum of values in an array in python. I am trying to solve this on a coding platform but it does not print any value. Why my code is not giving any kind of Output???

CodePudding user response:

Your simpleArraySum method doesn't have a return statement. It's only printing. You need a return statement in there for result to get something.

CodePudding user response:

Try return instead of printing the outputs.

def simpleArraySum(ar_count,ar):
    if ar_count == len(ar):
         return sum(ar)
    else:
        return "Invalid Input"
  •  Tags:  
  • Related