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.
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.
What happens when this print statement is executed: print print_grades(grades)
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)
That causes the function print_grades() to run with the argument, grades.
When print_grades(grades) runs, each of the grades is printed, for that is exactly what that function does.
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.
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âŚ
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)
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.
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.