0

I'm trying to understand how to properly define a function using the def().

I have come across question about using print() and return under def.

Now here are the two scripts I wrote:

def study_time(math,english,literature,chemistry):
    total = math + english + literature + chemistry
    short = 10 - total
    if total >= 10:
       print ("You have completed today's study goal!")
    else:
       print ("Keep going! You are still " + str(short) + " hours short!")

result = study_time(2,3,1,4)
print (result)

For this one I used print(), and when I called the function it returned two values: 1. The value I want it to present, and 2. None

def study_time(math,english,literature,chemistry):
        total = math + english + literature + chemistry
        short = 10 - total
        if total >= 10:
           return ("You have completed today's study goal!")
        else:
           return ("Keep going! You are still " + str(short) + " hours short!")

result = study_time(2,3,1,4)
print (result)

For this other one I used return, and the function returned only 1 value without the None

I'm just very curious what might explain the difference

Harry
  • 11
  • 1
  • Welcome to Stack Overflow. [Please don't post screenshots of text](https://meta.stackoverflow.com/a/285557/354577). They can't be searched or copied, or even consumed by users of adaptive technologies like screen readers. Instead, paste the code as text directly into your question. If you select it and click the `{}` button or Ctrl+K the code block will be indented by four spaces, which will cause it to be rendered as code. – ChrisGPT was on strike Jul 09 '22 at 12:58
  • When a function ends it will implicitly return None **unless** there is an explicit *return* of some value. Your second function will not return None. Your first function implicitly returns None – DarkKnight Jul 09 '22 at 13:18
  • 1
    Thanks Chris! I have edited the post according to the guidelines! – Harry Jul 09 '22 at 13:20

0 Answers0