Help!

This is the place for queries that don't fit in any of the other categories.

Help!

Postby lmp725 » Sat Apr 27, 2013 3:23 am

Code: Select all
ef 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()



this is the result I am getting

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
lmp725
 
Posts: 13
Joined: Sun Apr 14, 2013 6:27 pm

Re: Help!

Postby metulburr » Sat Apr 27, 2013 3:32 am

its not a good idea to name a variable name and a function name the same:
Code: Select all
gr = grade(scores[i],best)
print("Student", i, "scores is", scores[i], "and grade is", gr)


Code: Select all
metulburr@ubuntu:~$ python3 forum5.py
Enter scores:100
Traceback (most recent call last):
  File "forum5.py", line 18, in <module>
    main()
  File "forum5.py", line 16, in main
    grade = grade(scores[i],best)
UnboundLocalError: local variable 'grade' referenced before assignment
metulburr@ubuntu:~$ sudo vim forum5.py
[sudo] password for metulburr:
metulburr@ubuntu:~$ cat forum5.py | grep gr
def grade(i,best):
        gr = grade(scores[i],best)
        print("Student", i, "scores is", scores[i], "and grade is", gr)
metulburr@ubuntu:~$ python3 forum5.py
Enter scores:100
Student 0 scores is 100 and grade is A
metulburr@ubuntu:~$
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1312
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 5 guests

cron