2 Replies - 673 Views - Last Post: 25 January 2011 - 07:43 AM Rate Topic: -----

#1 samstrongng  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-January 11

Function Not Working

Posted 25 January 2011 - 02:40 AM

M trying to write a code that shows when one is entitled to retire. but the code seems not to be working.any assistance please?
Here is my python code:

def can_retire(age, isMale, isFemale, canretire, gender):
    return (isMale != True) and \
           ((age>60 and canretire)\
            or (canretire / 65>= isFemale)
def print_result(output, expected):     
           
    if output == expected:
            print 'can retire'
    else:
            print 'can retire'
def can_retire():
            print_result(can_retire(isMale, 60), True)
            print_result(can_retire(isMale, 59), False)
            print_result(can_retire(isFemale, 65), True)
            print_result(can_retire(isFemale, 64), False)
if __name__ == '__main__':
    can_retire()



MOD EDIT: Changed title from "Look at this please". Please do not use titles like that, use a descriptive title like that to which I've changed yours.

Edited by Dogstopper: :code:

This post has been edited by JackOfAllTrades: 25 January 2011 - 05:52 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Function Not Working

#2 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Function Not Working

Posted 25 January 2011 - 04:08 AM

First, you've defined can_retire() twice. Function definitions should be uniquely named.
Second, your calls to can_retire() on lines 12-15 are incorrect. Look at the first function definition, then look at the arguments you are passing, there is a rather large discrepancy.
Was This Post Helpful? 1
  • +
  • -

#3 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Function Not Working

Posted 25 January 2011 - 07:43 AM

Your code doesn't make a lot of sense to be honest.

I think what your code is trying to say is that Men can retire at the age of 60 and women can retire at the age of 65. My first question is, why would you want a isMale, isFemale, and gender variable? why not just have a variable called isMale and get rid of the others.

Variable Value Meaning
isMale True It's a male
isMale False It's a female

One boolean is all you need.

(isMale != True) and ((age>60 and canretire) or (canretire / 65>= isFemale)

You're trying to do a lot in 1 line of code. While this is acceptable, there's nothing wrong with expanding it. At this point in your programming career, focus on making understandable code. Assuming men can retire at the age of 60 and women at 65, this code will do the same thing but will be far more readable:
def can_retire(age, isMale):
    if isMale:  #same as if isMale == True:
        return age>=60

    else:  
        return age >=65

This code is much cleaner, albeit a little longer.

Now look at:
def print_result(output, expected):     
           
    if output == expected:
            print 'can retire'
    else:
            print 'can retire'


No matter what, you'll print out 'can retire'. I am assuming this is not as you intended. It would be redundant for me to proceed to point out the stuff Motoma already discussed. I would really suggest taking a step back and checking out some helpful Python resources.

This post has been edited by atraub: 25 January 2011 - 11:09 AM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1