Control flow - Else if statementss

Was wondering, can I put a print statement inside of a function to print the returned argument (in this case the string) of a variable which was provided by that function?

Here’s my code:

grade = 86
letter_grade = ""


def grade_to_lettergrade(grade):  
  global letter_grade
  if grade >= 90:
    letter_grade = "A"
  elif grade >= 80:
    letter_grade = "B"
  elif grade >= 70:
    letter_grade = "C"
  elif grade >= 60:
    letter_grade = "D"
  else:
    letter_grade = "F"
  return letter_grade

Is there a way to print letter_grade ?

1 Like

why are you using global?

why would you do this? Why not simple print the returned value?

2 Likes

Well, because I declared the variable outside the function (probably shouldn’t have done that)
In any case, how would that code look like with the print inside it (I tried, but it gave me errors )

I tried this now, I didn’t get any errors this time, but nothing got printed

grade = 86

def grade_to_letter(grade):  
  if grade >= 90:
    letter_grade = "A"
  elif grade >= 80:
    letter_grade = "B"
  elif grade >= 70:
    letter_grade = "C"
  elif grade >= 60:
    letter_grade = "D"
  else:
    letter_grade = "F"
  return letter_grade
  print(letter_grade)

return hands back data to caller (function call), which signals that the function is done executing, so any code after return is not reached/unreachable

why not simply print the returned value when you call the function?

2 Likes

Oh I see, but even after typing print(grade_to_letter) after the function it still doesn’t print anything, (an example code where the letter gets printed would be really helpful)

can I see a full code of where you attempt this?

calling/invoking a function requires parentheses (which you did correctly with print(), you don’t with grade_to_letter()), the argument is also missing.

1 Like

Here’s what I tried:

grade = 86

def grade_to_letter(grade):  
  if grade >= 90:
    letter_grade = "A"
  elif grade >= 80:
    letter_grade = "B"
  elif grade >= 70:
    letter_grade = "C"
  elif grade >= 60:
    letter_grade = "D"
  else:
    letter_grade = "F"
  return letter_grade

  print(grade_to_letter(grade))

Got nothing in the terminal.

The function call is (nested) within the (body of the) function. Why?

Really? I thought return ends the function so it doesn’t matter where I put the print function as long as it’s after the return.
Anyway, can you try out this and give me an example code?

If you want to print the output of the function, you need to put the function call inside a print statement like this: print(grade_to_letter(grade))

what happens when you comment out the global variable, call the function & pass 86 as the argument through the function?

ie:
print(grade_to_letter(86))

(with proper indentation)

You have all the logic defined inside the function. Think of the parameter (ie: grade) of the function as an empty variable. When you call the function you should be able to pass through any argument, be it 86 or 75 or 60, etc. which would then tell you the grade.

Indention, which determines nesting, certainly matters. To which block the code belongs.

2 Likes

In more complex functions return is nested into loops and conditionals to end the function when the desired results are found. In this function, anything nested after the return will be ignored since it will always return.
Here’s a function just to demonstrate, but there’s no reason to be typing the same thing over and over.

grade = 86

def grade_to_letter(grade):  
  if grade >= 90:
    letter_grade = "A"
    print("A")
    return letter_grade
  elif grade >= 80:
    letter_grade = "B"
    print("B")
    return letter_grade
  elif grade >= 70:
    letter_grade = "C"
    print("C")
    return letter_grade
  elif grade >= 60:
    letter_grade = "D"
    print("D")
    return letter_grade
  else:
    letter_grade = "F"
    print("F")
    return letter_grade
  

student_grade = grade_to_letter(grade)