If my input is greater than or equal to 1.0, then the result will be okay. But, if my input is less than 1, there will be the error like:
Traceback (most recent call last):
File “script.py”, line 13, in
print(grade_converter(0.8))
File “script.py”, line 11, in grade_converter
return grade
UnboundLocalError: local variable ‘grade’ referenced before assignment
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!
I know one way to change global variable inside a function is to use the keyword ‘global’ and define the variable you intend to use. Example in this case:
def grade_converter(gpa):
global grade
…
Doing it this way will return the grade value when gpa is below 1.0. But I too don’t understand why it gives error. grade is defined globally, so if no condition is met, it should access the value defined globally.
Maybe some experienced programmer here can answer this.
You are right but in his case, if no condition is met, then grade is never set and the function should use the globally defined grade, which it doesn’t in this case. And that’s confusing me.
I had that exact question : “Why is the default grade “F”? Is there a reason for this?”
Thanks for your answer, even I’m not totally sure I get it, at least It is a start.
The default grade is "F" in this exercise, because the if and elif blocks have previously handled all the other possibilities. If the grade is not "A", "B", "C", or "D", then "F" is the only possibility that remains.
Though the instructions do not ask you to do it this way, you could rewrite the function so that the default is "A", as follows:
dont know why but your answer is like using other planet’s language, what is foo, why there is +=1 , global is a syntax?,
sr but your answer alw confuse me.
"When a parameter has the same name as a variable defined outside, instead of the function using the variable defined outside, it will only reference the value that was passed to the parameter. So, parameters will be used over variables of the same name within a function. "
I guess here the rule is similar although “grade” is not a parameter?
I believe, if you assign values to any variable inside function, it is treated as a local variable. So, it is treating ‘grade’ as local variable instead of global one. Try changing the variable name while assignment inside function as following:
grade = "F"
def grade_converter(gpa):
if gpa>=4.0:
gr = "A"
elif gpa>=3.0:
gr = "B"
elif gpa>=2.0:
gr = "C"
elif gpa>=1.0:
gr = "D"
return grade
print(grade_converter(0)) #output F
Apart from assigning values, if you perform any other operation it works fine.
Example:
It works fine…
y=10
def experiment(x):
if x==10:
y
return y
print(experiment(0))
This gives same error…
y=10
def experiment(x):
if x==10:
y=1
return y
print(experiment(0))
So true what you said at the end there. Setting a F as default reflect creates an anomaly in the rules you just wrote evaluate numbers. keep things simple, but no more simpler than it is.
The program correctly returns F for me using your syntax.
grade = “F”
def grade_converter(gpa):
if gpa>=4.0:
grade = “A”
elif gpa>=3.0:
grade = “B”
elif gpa>=2.0:
grade = “C”
elif gpa>=1.0:
grade = “D”
gpa = 0.88
return grade
Defining the grade variable inside the Function with an ‘else’ statement would be best. Whenever you call the function grade_converter(), the grade variable it is limited to is the local variable defined in the Function and it does not reference the value of the Global variable where grade = “F”. So the best way to write the function is with an else statement.