Reputation: 39
when i run
profitable_gamble(0.2, 50, 9)
with this function there is no output
def profitable_gamble(prob, prize, pay):
if prob * prize > pay:
return "true"
else:
return "false"
however when i change the function to use print()
def profitable_gamble(prob, prize, pay):
if prob * prize > pay:
print("true")
else:
print("false")
I get "true" as expected. how can I get this to happen while using return rather than print?
Upvotes: 1
Views: 389
Reputation: 2580
This is what you think your code should do:
def profitable_gamble(prob, prize, pay):
if prob * prize > pay:
return "true"
else:
return "false"
returned_value = profitable_gamble(0.2, 50, 9)
print(returned_value)
But then you never tell it to print anything, so it doesn't. If you only call this:
profitable_gamble(0.2, 50, 9)
Then your function returns "true" or "false" nonetheless.... you just never do anything with it, so it just goes to die in the call stack.
EDIT: you may also be confused because of the use of command-line (or something like Ipython notebooks). In those interactive shells, if you do:
profitable_gamble(0.2, 50, 9)
>> "true"
Then indeed it does show the value on screen. However, this is just because you're using a shell which (presumably) outputs the result of the call to the console for you. So essentially that shell (be it a terminal or some interactive notebook) does for you the additional print() statement....
As a note - there is little reason in general to do something like
return "true"
In python. Generally you would want to just use the boolean:
return True
(notice the capital T).
Especially since you can anyway do print(some_boolean_variable) or str(some_boolean_variable).... The added benefits being that it's much easier to test a boolean value than to compare the string "true" everytime...
Upvotes: 3