Your code is OK for a simple case with 3 values.
If you had more values, it would quickly go out of hand.
Essentially, you want to sum values until you find an "illegal" value.
We can implement this in terms of
def lucky_sum(a, b, c):
>>> lucky_sum(1, 2, 3)
>>> lucky_sum(1, 2, 13)
>>> lucky_sum(1, 13, 3)
>>> lucky_sum(13, 1, 3)
illegal = 13
return sum(itertools.takewhile(lambda x: x != illegal, [a, b, c]))
This can be easily extended to support a more general case with arbitrary number of arguments by replacing a, b, c with
return sum(itertools.takewhile(lambda x: x != illegal, args))
Please include doctests like I did in this answer and also in my previous answers to your recent questions.
If your script is in a file named
then you can run the doctests with:
python -m doctest lucky_sum.py
This is very handy.
Covered with doctests, you can freely rewrite your implementation and easily verify it still works or not.
If you include doctests in your questions,
you make the reviewer's job a lot easier.
It would also show that you're learning from answers.