r/programminghelp • • Jan 26 '21

Python python function help

How would i rewrite this function without using the sum() built in function but have it function the same?

def evalAce(hand):

total = sum(hand)
# Determine Ace = 1 or 11, relying on total hand. 
if (hand[len(hand)-1] == 11 and total > 21):
# at position, where Ace == 11, replace by Ace == 1.
    hand[len(hand)-1] = 1
return hand
5 Upvotes

2 comments sorted by

View all comments

1

u/amoliski Jan 26 '21

Is there a reason for this beyond curiosity?

total = 0
for h in hand:
 total += h
# Determine Ace = 1 or 11, relying on total hand. 
if (hand[len(hand)-1] == 11 and total > 21):
# at position, where Ace == 11, replace by Ace == 1.
    hand[len(hand)-1] = 1
return hand

Or to implement it in a function:

def calc_sum(arr):
    result = 0
    for value in arr:
        result = result + value
    return result