Why does my print_grades( ) not work?

Question

Why does my print_grades( ) not work?

Answer

Some common issues with this exercise include:

  1. Using return inside of your print_grades() function, rather than printing the values
  2. Not using the print_grades() function at the end of your program (outside of the function with no indentation) and passing it the grades list
  3. Looping incorrectly by printing the entire list each time, rather than just the loop variable for the single grade
grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

def print_grades(grades_input):
  for grade in grades_input:
    print grade
    
print print_grades(grades)

After running the code, an additional output after ‘50.5’ is seen in the console as a ‘None’ value. Where is this coming from? The list looks fine and I think my code is good for the exercise.

2 Likes

None is the implicit returned value by the function.

1 Like
def print_grades(grades_input):
  for grade in grades_input:
    print grade

How can I have the code in one line? I tried

def print_grades(grades_input):
  print grade for grade in grades_input

But that prints the list in one line

if the body of the loop is a single line, you can do:

def print_grades(grades_input):
  for grade in grades_input: print grade
1 Like

tell me what did i do wrong?

Did you follow the step in the instructions that begins with the word, “After”?

1 Like

whats implicit value by the function??

Do you mean, “What value does the function return”?

If so, the function returns the value None. The function has no return statement, and, in Python, every function having no return statement returns None.

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

def print_grades(grades_input):
for grade in grades_input:
print grade

print print_grades(grades)

In this, I got the whole thing printed out but I also got a none, at last, why is that?

1 Like
grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

def print_grades(grades_input):
    for grade in grades_input:
        print grade

print print_grades(grades)

What happens when this print statement is executed:
print print_grades(grades)

  1. The print statement needs to know what to print, so it executes the expression to its right, hoping to be returned a value to print. That expression is print_grades(grades)
  2. That causes the function print_grades() to run with the argument, grades.
  3. When print_grades(grades) runs, each of the grades is printed, for that is exactly what that function does.
  4. When the function has printed out all of the grades, it, having no return statement, returns the value None, as I explained in my other post.
  5. That value, None, it what is passed to the print statement as the value that should be printed, so print sends that value (actually a stream of characters representing that value as a string) to the screen…
3 Likes

thanks that’s some explanation…
+respect

1 Like

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

def print_grades(grades_input):
for grade in grades_input: print grade

return print_grades(grades)

When I hit run it keeps repeating the list of grades seemingly ad infinitum on new lines instead of listing it once & stopping. Any ideas where I’m going wrong?

grab your finger, point at the top of the file, do everything it says, moving your finger down one step after each instruction unless the instruction is to go elsewhere
or is there anything there you don’t know what it does? if so, then you’d have to stop and figure that out anyway before you can use it to describe a program

though, as it is, that program will not print anything at all. you ran something else (formatting issues aside)

https://repl.it/repls/TalkativeFilthyMacrolanguage

I don’t see what the issue is and I don’t follow what you’re trying to say. Assume that the proper indentation is there.

No such thing as proper. Indentation is part of the code, you can write different code.
If it was optional then it would be optional.

I edited in a link to where you can run your code. As you can see if you run it, it does not do what you describe.

You might want to call your function, that’s when they run. And then, yes, it’ll do something like that.

I argue that you know what everything there does (look at it. anything alien? no? good to go, then.). If you don’t do the same thing it may be that you’re executing what you think it should do instead of what it says, bypassing the code and running your own different program that you’re envisioning in your mind. So carefully following each instruction will lead to repetition.

Keep in mind that you can also edit it to see what effect things have.

Indeed, printing can also tell you what order things are done, you might insert things like:

print 1
print 2
print 3

all over, that would tell you exactly where it jumps and what the instruction before it was.

I’ve tried playing around in the editor for a couple of days with different things and I still can’t figure out where I’m going wrong. Am I missing something crucial?

How many times does your function get called?
At what times are you calling your function?

If you were to call your function at a time that happens more than once, then your function would happen more than once too.

So, when you execute your function in your head, each time you reach a point where the code says to call the function, you’d have to do that. And if you keep visiting that point then you’ll keep calling your function.

I still don’t get it, man

If you look at your error message, there is a list of functions that are waiting to finish (call stack)

Your function appears there a lot. You are calling your function over and over and over.

What does a function call look like?
Where do you do that in your code?


When you read code and encounter a function call, you would need to continue reading at the start of that function. So it says right there in the code why it repeats. So long as you do actually execute the function calls that are in your code, you will never be able to stop executing.

But you know how function calls work already, don’t you? You’re somehow bypassing it when you’re reading it, or not reading it.
But a computer will execute it the way it says.

1 Like