From what I’ve read about writing clean code and assigning values to global variables, the main reason (besides the fact that it results in an UnboundLocalError and breaks your code) not to set grade to “F” here is that there is no compelling reason to do so, and it makes the code just that slight bit more confusing to all who might have to read it subsequently. Whoever tries to rewrite your code later for whatever reason will have to ask “Why is the default grade “F”? Is there a reason for this?”
The only reason to do it in the first place is that it (potentially) saves one line of code (we’re overlooking the fact that it makes the code snippet non-functional). And that’s a reason, but not a compelling one.
Does it matter on 10 lines of code if there’s a moment’s hesitation on the part of whoever looks at your code later? On 10 lines, no. But multiply that out by function after function, line after line - you write a simple 1000-line program (okay, that’s not sooo simple, but you get what I’m saying), and if you’re careless about how the code is written after every step, suddenly you have 100 different places where anyone refactoring or otherwise dealing with your code has to stop and say “Why did the coder do this?”
They say that most coders/developers spend over half their time reading code rather than writing it. Sounds plausible to me.
So think of the generations to come who will need to calculate their grades using your function!
Have a good one, and happy coding everyone.