-3

I am actually working on python on coding bat. This is the problem that I am having issues with: http://codingbat.com/prob/p107863. Not sure if the problem is actually with them. Here is my code. It is used to add up all of the inputs that are not equal to 13. There is probably a better way to do it. However, what I really want to know is how to evaluate the next IF statement if the previous IF statement's condition is not met.

def lucky_sum(a, b, c):
  results=0
  if a != 13: 
    results= results + a
  if b != 13: 
    results= results + b
  if c != 13: 
    results= results + c
  return results  #EDIT: Indented this 2 spaces to correct OP formatting error -sequoia

If you run this lucky_sum(1, 13, 3) it returns 1 instead of 4. I am sure this is something very easy to fix.

3
  • your code is fine! maybe you only need to indent it properly!
    – Guido
    Jul 17, 2013 at 17:55
  • @Neil your code doesn't take into account part of the problem, see my updated answer. Jul 17, 2013 at 18:16
  • @Neil as a future reference, also consider having a look at Difference between multiple ifs and elifs. Here, multiple ifs is a valid approach but that may not always be the case.
    – Schorsch
    Jul 17, 2013 at 18:21

4 Answers 4

6

A better way to write your function would be to use non-keyword arguments. And just use sum() function over a generator expression to return the sum.

>>> def lucky_sum(*nkwargs):
...     return sum(x for x in nkwargs if x != 13)

>>> lucky_sum(1, 13, 3)
4

In your original code, there is an issue of indentation error. Your return statement is not properly indented.


As per your modification, the above code, and the one in your question won't work for the Coding Bat problem. There you need to stop adding once you find a value equal to 13.

For that you can modify your original function return the sum from the if block, where value is 13. I'll give the corresponding modification for my function here:

>>> def lucky_sum(*nkwargs):
...     try:
...         index = nkwargs.index(13)
...         return sum(nkwargs[:index])
...     except ValueError:
...         return sum(nkwargs)

>>> lucky_sum(1, 3, 4)
8
>>> lucky_sum(1, 3, 13,  4)
4
9
  • 1
    @vikingosegundo. You would have to pass arguments in a list in that case. With non keyword arguments, you can just pass the values.
    – Rohit Jain
    Jul 17, 2013 at 17:58
  • yeah, I realized that the moment i pressed send Jul 17, 2013 at 17:59
  • This should rather be a comment.
    – Gumbo
    Jul 17, 2013 at 18:02
  • @Gumbo. Sorry? I didn't understand. Was it for the answer?
    – Rohit Jain
    Jul 17, 2013 at 18:04
  • @RohitJain Neil wants to know why his function doesn’t work: “what I really want to know is how to evaluate the next IF statement if the previous IF statement's condition is not met.” But your answer simply does not answer this question.
    – Gumbo
    Jul 17, 2013 at 18:10
2

what I really want to know is how to evaluate the next IF statement if the previous IF statement's condition is not met.

If an if statement's condition isn't met it will skip over the indented logic and move to the next line of equal indent to the if statement, which in this case is another if statement. So your code works as intended.

Just indent the return statement properly.

If you run this lucky_sum(1, 13, 3) it returns 1 instead of 4.

Not true. Your code returns 4, but it's supposed to return 1! You're missing this piece of the problem:

However, if one of the values is 13 then it does not count towards the sum and values to its right do not count.

So if a value is 13 you won't want to add values to the right of that, either, which you do.

1
  • I feel dumb... haha. Thank you. I was reading the results incorrectly.
    – Neil
    Jul 17, 2013 at 18:20
1

I don't believe that the code posted is your real code.

def lucky_sum(a, b, c):
  results=0
  if a != 13: 
    results= results + a
  if b != 13: 
    results= results + b
  if c != 13: 
    results= results + c
return results #<--- This would result in an error

However, subject to indenting the return properly, your code will indeed evaluate each if statement independently of whether a previous if statement body was entered. Correctly indented, you will indeed get 4 out of lucky_sum(1, 13, 3) (see: http://ideone.com/FIvEnk).

However, a better way to do this would be:

def lucky_sum(a, b, c):
    acc = 0
    for n in (a,b,c):
        if n != 13: acc += n
    return acc

You can also take a variable number of arguments using the syntax def lucky_sum(*some_args):. I leave it to you to figure out how to use that in your function.

Your problem is that you have misunderstood the tutorial website you are using: it is asking you to do the exact opposite of what you say you want, and it wants the result you (incorrectly) say your code produces, not the result you incorrectly believe it wants.

The following code meets the specification they request:

def lucky_sum(a, b, c):
    acc = 0
    for n in (a,b,c):
        if n == 13: break
        acc += n
    return acc
5
  • I added more detail to the beginning of my question.
    – Neil
    Jul 17, 2013 at 18:13
  • @Neil I don't care what course you're doing. The correctly indented code does what you say you want it to.
    – Marcin
    Jul 17, 2013 at 18:15
  • @Neil In fact, here's the proof: ideone.com/FIvEnk
    – Marcin
    Jul 17, 2013 at 18:16
  • I originally thought that something was wrong with my code because the website was showing that my result 1 instead of 4. That is why I posted here. I don't have python installed on this computer so the only way I could test the cod was using their website and seeing what it says my results are.
    – Neil
    Jul 17, 2013 at 18:17
  • @Neil Having looked at that website, they are asking you to do the exact opposite of what you say you want, and what your code actually does. The result they are looking for is not the result you say you want.
    – Marcin
    Jul 17, 2013 at 18:20
0

"However, what I really want to know is how to evaluate the next IF statement if the previous IF statement's condition is not met."

The answer is to use else and/or elif:

if a != 13:
    print("a is not 13, but b and c might be")
elif b != 13:
    print("b is not 13, but a is, and c might be")
elif c != 13:
    print("c is not 13, but a and b are")
2
  • The original code evaluates if the previous if's condition is not met; this does so if and only if the original condition is not met.
    – Marcin
    Jul 17, 2013 at 20:23
  • Yes, it does, and (1) the OP also stated that the original code wasn't what he wanted; (2) Python doesn't have an iff statement; and (3) asking "how do I evaluate the next if statement if the previous statement's condition is not met, but also evaluate the next if statement if it is?" is a complete non-question.
    – Crowman
    Jul 17, 2013 at 20:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.