### Thread: Code Help!!!!

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Apr 2013
Posts
3
Rep Power
0

#### Code Help!!!!

def grade(i,best):
if i >= best - 10:
return 'A'
elif i >= best - 20:
return 'B'
elif i >= best - 30:
return 'C'
elif i >= best - 40:
return 'D'
else:
return 'F'
def main():
scores = [int(i) for i in input("Enter scores:").split()]
best = max(scores)
for i in range(0, len(scores)):
grade = grade(scores[i],best)
print("Student", i, "scores is", scores[i], "and grade is", grade)
main()

my result is this:

Traceback (most recent call last):
File "LiveLabTempFileWithLiveLabInput.py", line 29, in
main()
File "LiveLabTempFileWithLiveLabInput.py", line 27, in main
grade = grade(scores[i],best)
UnboundLocalError: local variable 'grade' referenced before assignment
2. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
6
Rep Power
0
read about indentation
3. Originally Posted by lmp725
my result is this:

Traceback (most recent call last):
File "LiveLabTempFileWithLiveLabInput.py", line 29, in
main()
File "LiveLabTempFileWithLiveLabInput.py", line 27, in main
grade = grade(scores[i],best)
UnboundLocalError: local variable 'grade' referenced before assignment
Grade is defined as the name of your function, inside the loop you try to assign it a value and at the same time call it as a function.
Code:
```for i in range(0, len(scores)):
grade = grade(scores[i],best)```
This will not work because once assigned a value during the first iteration, grade will no more be considered as a function for the next iterations inside the for loop.
Here is another example which shows the problem
Code:
```>>> def increment(p_intVal):
...     return p_intVal + 1
...
>>>
>>> increment
<function increment at 0x7f460c652848>
>>>
>>>
>>> increment(12)
13
>>> increment
<function increment at 0x7f460c652848>
>>>
>>> increment = increment(12)
>>>
>>> increment
13
>>>
>>> type(increment)
<type 'int'>
>>>```
Instead, you can try the following

Code:
```def grade(i,best):
if i >= best - 10:
return 'A'
elif i >= best - 20:
return 'B'
elif i >= best - 30:
return 'C'
elif i >= best - 40:
return 'D'
else:
return 'F'

def main():
scores = [int(i) for i in input("Enter scores:").split()]
best = max(scores)
for i in range(0, len(scores)):
gradeVal = grade(scores[i],best)
print("Student", i, "scores is", scores[i], "and grade is", gradeVal)

main()```
And a test

Code:
```\$ python -tt myscript.py
Enter scores:"10 50 100 70 80"
('Student', 0, 'scores is', 10, 'and grade is', 'F')
('Student', 1, 'scores is', 50, 'and grade is', 'F')
('Student', 2, 'scores is', 100, 'and grade is', 'A')
('Student', 3, 'scores is', 70, 'and grade is', 'C')
('Student', 4, 'scores is', 80, 'and grade is', 'B')
\$```
And of course always use CODE tags when you ask a question on the forum, it makes your code more clear and readable for those who try to read your code and understand what is the problem.
Last edited by dariyoosh; April 27th, 2013 at 03:27 AM.
4. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
194
Rep Power
3
This will not work because once assigned a value during the first iteration, grade will no more be considered as a function for the next iterations inside the for loop.
Actually it doesn't even get past the first iteration in this case.
As soon as the interpreter sees:
Code:
`grade =`
within the main function it decides that grade is a variable local to the function. When it gets to the:
Code:
`grade(scores[i],best)`
part of that line it doesn't even look in the global scope for the function grade. This is why the error that he receives is:
Code:
`UnboundLocalError: local variable 'grade' referenced before assignment`
and not:
Code:
`TypeError: 'str' object is not callable`
Anyway, the solution is still the same. Don't use the same name as the function you want to call.

-Mek