1
def Fitness(a, b, c):  
    if ((a&b&c) >= 4) & ((a+b+c) >= 13):  
        return('Gold')  
    if ((a&b&c) >= 3) & ((a+b+c) >= 10):  
        return('Silver')   
    if ((a&b&c) >= 2) & ((a+b+c) >= 07):  
        return('Pass')  
    else:  
        return('Fail')

now the Problem is when Fitness(2,2,5) is given, the control jumps to default ie. 'Fail'. Where is the actual output is 'Pass'. ?

1
  • 1
    Could you explain the logic you're trying to implement? Commented Aug 7, 2011 at 11:41

5 Answers 5

18

Note that

a&b&c >= 2

is different from

a>=2 and b>=2 and c>=2.

I think you mean the second one, i.e. all values are larger than two. (The first one does a binary and with all your values and compares this to the value two.)

Sign up to request clarification or add additional context in comments.

1 Comment

Ah! +1 for best use of crystal ball today!
6

Use and instead of & (binary and). And don't write 07 - numbers starting with 0 may be interpreted as octal depending on your version of Python.

Together with Howard's inspiration, I'd suggest this:

def Fitness(a, b, c):
    if all(x>=4 for x in (a,b,c)) and (a+b+c) >= 13:
        return('Gold')
    if all(x>=3 for x in (a,b,c)) and (a+b+c) >= 10:
        return('Silver') 
    if all(x>=2 for x in (a,b,c)) and (a+b+c) >= 7:
        return('Pass')
    return('Fail')

Also, it's sad that you're not awarding a bronze medal...

5 Comments

I'm not sure this should be causing the problem. True is 1 and False is 0, so True & True should still give True, True & False should still give False, etc.
@Chris: Right. I've just checked that after many subtle corrections to his code to get it to run at all. Clarification by the OP needed.
hmm...(a&b&c)>= no, causes problems, if a=b it goes to default condition. i m not sure!!
used (a>=no) & (b>=no) & .... instead, i like to know why the latter doesn't work
Type 2&2&5 into a Python shell and see for yourself. 0b010 binary and 0b101 is 0. Don't confuse logical and with binary &!
3

Where is the actual output is 'Pass'.

No it isn't. 2 (0b010) & 2 & 5 (0b101) is 0, so the expressions will all fail even if you change the & separating the two terms to and. Perhaps you meant to use a completely different expression?

1 Comment

By "where the actual output is", he seems to mean "where the actual output should be".
2

I'd post this as a comment to Tim Pietzcker's answer, since it's just an improvement on it, but the comment field would mess up the formatting, so I'm making it a separate answer.

def fitness(*args):
    arg_sum = sum(args)
    smallest_arg = min(args)

    if smallest_arg >= 4 and arg_sum >= 13:
        return 'Gold'
    if smallest_arg >= 3 and arg_sum >= 10:
        return 'Silver'
    if smallest_arg >= 2 and arg_sum >= 7:
        return 'Pass'
    return 'Fail'

First the important stuff:

  1. Enclosing the return value in parenthesis like in return('Gold') is not wrong per se, but will confuse the reader; return is a statement, not a function. It works nontheless because the parenthesis are in effect silently ignored. If what you wanted was in fact to return a tuple with a single string element, do return ('Gold',).
  2. DRY: Compute the sum and smallest element of the arguments once, and use the computed values in the if blocks, as shown above.
  3. Since the arguments are all treated equally and together, gather them in an *args tuple. This will also generalize the function to work with any number of arguments. If that should not be allowed, add this to the beginning of the function:

    if len(args) != 3:
        raise TypeError("Need exactly 3 arguments, got {0}".format(len(args)))
    

Finally I'll just mention that functions should have lower_case_names according to the python style guide. Following the style guide is of course not mandatory, just recommended. :)

Comments

-1
def fitness(*args):
    arg_sum = sum(args)
    smallest_arg = min(args)

    if smallest_arg >= 4 and arg_sum >= 13:
        return 'Gold'
    if smallest_arg >= 3 and arg_sum >= 10:
        return 'Silver'
    if smallest_arg >= 2 and arg_sum >= 7:
        return 'Pass'
    return 'Fail'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.