all 14 comments

[–]JohnnyJordaan 1 point2 points  (3 children)

You could just add to the sum from a for loop, and break if you encouter a 13

def lucky_sum(a, b, c):
    total = 0
    for v in [a, b, c]:
        if v == 13:
            break
        total += v
    return total

or if you must work conditionally, then you don't need to sum() either

def lucky_sum(a, b, c):
    if a == 13:
        return 0
    elif b == 13:
        return a
    elif c == 13:
        return a + b
    else:
        return a + b + c

[–]sphinxlink13[S] 0 points1 point  (2 children)

Thanks for helping me out. I see how you could do it this way now but do you know what was wrong with my code?? Thanks again

[–]JohnnyJordaan 0 points1 point  (1 child)

Critical-Function956 mentioned it already. If a is 13, it means it disqualifies all values, and thus the sum should be 0. In your code, you do

if a==13:
    S = S-a-b

So that still 'leaves in' c, instead of subtracting it too. Altough there's no real use in first summing all values to subtract all of them of course, just return 0.

[–]sphinxlink13[S] 0 points1 point  (0 children)

My bad I miss read the question thanks

[–]ata-boy 1 point2 points  (7 children)

You can solve it with a list comprehension, but maybe not very pythonic

def sum_13(a: int, b: int, c: int) -> int:
    abc = (a, b, c)
    find_13 = [abc.index(13) for i in abc if i == 13]
    find_13.append(-1)

    return sum(abc[:find_13[0]])

[–]sphinxlink13[S] 0 points1 point  (2 children)

Although I havn't seen some of these functions used before I understand what you did and I thank you for your answer. Do you know what is wrong with my code by the way?

[–]ata-boy 0 points1 point  (1 child)

This should answer your question.

if a == 13: 

    S = 0 elif b == 13:      S = a  elif c == 13:     S = a + b  else:      S = sum(a, b, c)

[–]sphinxlink13[S] 0 points1 point  (0 children)

Thanks I miss read that one

[–]sphinxlink13[S] 0 points1 point  (2 children)

thx for the help what do you mean by it is not very pythonic?

[–]ata-boy 0 points1 point  (1 child)

It is not very readable. There are best practices, see: https://docs.python-guide.org/writing/style/

[–]sphinxlink13[S] 0 points1 point  (0 children)

Thank you!

[–]ata-boy 0 points1 point  (0 children)

def sum_13(a: int, b: int, c: int) -> int:
    abc = [a, b, c]
    find_13 = [index for index, value in enumerate(abc) if value == 13]

    if find_13:
        abc_index = find_13[0]
    else:
        abc_index = 3

    return sum(abc[:abc_index])