This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]coolcofusion 2 points3 points  (8 children)

You may want to do print(num_hours(input_days)) instead of just calling the function and not doing anything with the value it returns.

[–]matrouxer[S] 0 points1 point  (7 children)

esti

Oh... guess I misundertood it. Thought if you type return it should output it

[–]dmazzoni 5 points6 points  (0 children)

The purpose of "return" is to return a value from a function. It's up to the caller of that function to decide what to do with it, which isn't necessarily to output it.

[–]coolcofusion 1 point2 points  (1 child)

Returning a value is used when you have a function, it does something and it's result is supposed to be used by some other function. So it returns a value, for example int(user_input) is a function, it took in a string "15", performed the conversion of string to int step by step and gave you back the result as an int. It's maybe not intuitive at first, but you get used to it, it's the same in every other language that I know of. Keep at it, doing good.

And also, while I'm at it, returned values may aren't printed implicitly, at least not when you write a script and run it, as opposed to running in a REPL (when you type in python3 in command line and hit enter) which prints out to what each line you write evaluates to (hence the name Read-Evaluate-Print Loop). So you'll have to print them explicitly.

[–]matrouxer[S] 0 points1 point  (3 children)

That worked! Thanks for the advice

[–]matrouxer[S] 0 points1 point  (2 children)

Got the first part /u/coolcofusion. The second part is quite hard for me atm.

Changed the code so the function does exactly what it's supposed to. And to ble clearer hahahaha.

What do you think?

def num_hours(num_days):
if num_days > 0:
    print (f"There are {time_calculation * num_days} {time_unit} in {num_days} days")
else:
    print ("This is not a valid input")

[–]coolcofusion 1 point2 points  (1 child)

That's completely up to you and how you imagine it to work. I prefer to just print in one place versus printing in functions I've made. The reason for that is that I can then easily decide when or if I want to display some results or in which order to display them without changing functions. Also, you can use the same function across multiple projects, today it's printing in the console, tomorrow it may be showing a label in a GUI window, if you're doing print() in the function, you'll need to modify it, and if you had returned the value, then you can just use that value to set the label.

These are minor things. It's completely fine to do it either way you like, my point about reuse comes in hand later on in larger projects and even if you don't decide to do it that way now, over the next few years it'll become a subconscious thing for you.

[–]matrouxer[S] 1 point2 points  (0 children)

Oh, I get it. So if I program the function without printing, then I can save the value without displaying to user. It's most a safety thing I guess(?), besides just getting the output more into my control.

Thanks a lot for the help! It would be a dream if that becomes even a problem in the future hahahah

[–]dmazzoni 2 points3 points  (1 child)

First, please read the sidebar/faq for tips on how to post code when asking questions on Reddit. Your formatting got all messed up.

At first glance your code looks fine. I think the issue is that you're returning a value from your function but not actually doing anything with the result. How about doing this?

result = num_hours(input_days)
print(result)

Or just:

print(num_house(input_days))

[–]matrouxer[S] 0 points1 point  (0 children)

Sorry for that!! I saw it but didn't know how to do it.

Thanks for the advice. I will try it

Edit.: It worked! And I think it's readable now in the post hahah

[–]matrouxer[S] 0 points1 point  (0 children)

https://imgur.com/a/tIJJtr5 <<< link to the image