-2

So while running the codes on the following error is generated. It says that the NameError is left unhandled by the user code.

The error:

Traceback (most recent call last):
File "D:\3rd sem\Object Oriented Programming\Lab\VS\PythonApplication1\PythonApplication1\PythonApplication1.py", line 50, in <module>
main()
File "D:\3rd sem\Object Oriented Programming\Lab\VS\PythonApplication1\PythonApplication1\PythonApplication1.py", line 10, in main

grade=str(input("Enter the grade: "))
File "<string>", line 1, in <module>
NameError: name 'a' is not defined

The code goes like this:

classnum=int(input("Enter the num of classes: "))

def main():
totalcredit=0
totalgpa=0

for i in range(1,classnum+1):
    print "class", i
    credit=int(input("Enter the credit: "))
    grade=str(input("Enter the grade: "))
    totalgpa+=coursePoints(credit,grade)
    totalcredit+=credit

totalcourse=classnum 
semestergpa=totalgpa/totalcredit
print("Semester summary")
print("courses taken: ", classnum)
print("credits taken: ", totalcredit)
print("GPA points: ", totalgpa)
print("Semester GPA: ", semestergpa)

def coursePoints(Credit,Grade):
    if Grade == 'A+' or Grade == 'a+':
        return 4*Credit
    elif Grade == 'A' or Grade == 'a':
        return 4*Credit
    elif Grade == 'A-' or Grade == 'a-':
        return 3.67*Credit
    elif Grade == 'B+' or Grade == 'b+':
        return 3.33*Credit
    elif Grade == 'B' or Grade =='b':
        return 3*Credit
    elif Grade == 'B-' or Grade == 'b-':
        return 2.67*Credit
    elif Grade == 'C+' or Grade == 'c+':
        return 2.33*Credit
    elif Grade == 'C' or Grade == 'c':
        return 2*Credit               
    elif Grade == 'C-' or Grade == 'c-':                        
        return 1.67*Credit                
    elif Grade =='D+' or Grade == 'd+':                      
        return 1.33*Credit                
    elif Grade == 'D' or Grade == 'd':                        
        return 1*Credit                      
    elif Grade == 'D-' or Grade == 'd-':                                
        return 0.33*Credit                     
    else:
        return 0

main()

Can anyone help for solutions.

Thanks in advance.

Pa-won
  • 165
  • 1
  • 2
  • 10

1 Answers1

1

You seem to be using Python 2.x, so you should change

grade=str(input("Enter the grade: "))

To

grade = raw_input("Enter the grade: ")
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • 1
    @Pa-wonSunuwar Stack Overflow is not really designed for back-and-forth discussion. It is set up for small specific questions and answers. If you have a new question, please create a new post. However, that being said I suggest you google "python variable referenced before assignment" before making a new question, because this is a common mistake. – Cory Kramer Mar 06 '16 at 20:52
  • Thank you all for your suggestions – Pa-won Mar 06 '16 at 20:53