0

I'm writing a program that returns the train prices. When I run the function nothing is returning.

def transit(zone, ticket):
    fareLst = ['23', '11.5', '34.5']

    if zone < 2 and ticket == 'adult':
        return fareLst[0]

    elif zone < 2 and ticket == 'child':
        return fareLst[1]

    elif zone > 4 and ticket == 'adult':
         return -1


def main():
    transit(1, 'adult')   # the function is not returning the fareLst = 23


main()
2
  • Why are your fareLst values stored as strings? Shouldn't they be either float or Decimal? Commented Apr 28, 2014 at 0:33
  • @user3339203: you are supposed to accept one of the answers (unless none of them are good); you have asked many questions on SO and most of them don't have an accepted answer. Other people are putting their time into helping you; the least you can do is show your appreciation by giving them credit. Commented Apr 28, 2014 at 2:03

3 Answers 3

5

The function is returning the value; you're just not doing anything with it. You can print it if you want:

def main():
    print transit(1, 'adult')

return is not the same as printing something on the screen; return values are used like this, for example:

smth = transit(1, 'adult')

You don't want everything that you ever return to be printed out automatically.


Also, on another note, you should use proper floating point values to store floating point data, instead of strings:

fareLst = [23.0, 11.5, 34.5]

Also, note that if you call transit(5, 'child'), it will return nothing/None... so you might want this at the end of your if-elif block:

else:
    return -1

instead of:

elif zone > 4 and ticket == 'adult':
     return -1

...although None might also be a viable option for e.g. completely invalid input; but then again, an exception would be even better in that case.

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

Comments

2

If you only want to just print out the result, do the following:

def main():
    print transit(1,'adult')

or if you want to store the variable and print it later, do the following:

def main():
    result = transit(1,'adult')
    #do something
    print result

1 Comment

whoever downvoted this answer — why? there's nothing wrong or missing here.
0

Others have already mentioned that you're not storing the return value, but I'll also add that you have no default "base case" at the end. In this code:

def transit(zone, ticket):
    fareLst = ['23', '11.5', '34.5']

    if zone < 2 and ticket == 'adult':
        return fareLst[0]

    elif zone < 2 and ticket == 'child':
        return fareLst[1]

    elif zone > 4 and ticket == 'adult':
         return -1

what if zone is equal to 2, 3, or 4? What if zone is 4 but ticket is child? All of those would fall through to the end, and the default return value of a function is None. That's probably not what you want. If not, add something like return 20 or whatever a sensible default ticket price would be.

Also, it's not Pythonic to return sentinel values like in the final case above. A more Pythonic approach would be to raise a ValueError if the parameters are bad, then wrap your calling code in a try/except block. That leaves no room for ambiguity, and checking return values is a pain in the neck.

1 Comment

For the record: I actually did mention the missing default case in my answer ~13 min ago :P

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.