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

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

1

u/[deleted] Jan 26 '21

/u/Additional_Macaroon5

I see from your post history here that you're doing an assignment that requires you to not use any built in functions. You've posted here QUITE a few times asking for help like this and I'm not sure if you've noted you're working on assignment. Also, us just TELLING you how to solve these issues violates Rule #4. So, consider this your last warning.

Please, from this point forward, post any code attempts you have made to solve these issues on your own when requesting help. Given how much help you've gotten so far, I don't think it's too much to ask, but failure to do so will result in a suspension long enough make sure your assignment is due before you can return.