CGPA Calc Python

时间:2016-10-16 20:53:43

标签: python linux calculator

请帮帮我。我试图做一个CGPA计算器。但是有些人出错了我无法找到答案:'(我收到此错误消息说:

 Traceback (most recent call last):
      file "cgpa.py", line 60 in 7
           count_sem, total_gpa, credit = input_user().
      file "cgpa.py", line 53, in input_user
           total_gpa=total_gpa + gpa
 unboudndlocalerror:local variable "gpa" reference before assignment

我的编码或格式有什么问题吗?我希望当用户按1进入菜单时显示功能cgpa。当用户输入完成后循环应该结束然后进入菜单。

def menu(): 

 print "\n\n----CGPA MENU----\nChoose the option below:\n" 
 print "Enter 1 - Total CGPA"   print "Enter 2 - Total semester"  
 print "Enter 3 - Target Next GPA (if CGPA < 3.0)"
 print "Enter 4 - EXIT "    user_menu = raw_input ("Option: ")
    return user_menu      

def cgpa(a,b):
     average = a/b      

     return average

def input_user(): 
     while True:
            count_sem = 0
            total_gpa = 0
            credit = 0

            print "\n~To STOP enter finish~"

            user1 = raw_input ("Enter GPA: ") 
            if user1 == "finish":           break
            try:
                    gpa = float (user1) 
            except:
                    print "Invalid number"
                    continue

            user2 = raw_input ("Semester: ")
            try:
                    sem = int(user2)
            except:
                    print "Invalid number"
                    continue        

            user3 = raw_input ("Enter total credit hour: ")
            try: 
                    hour =    int(user3)
            except:
                    print "Invalid number"
                    continue

            count_sem = count_sem + 1
            total_gpa = total_gpa + gpa
            credit =    credit + hour

        return count_sem, total_gpa, credit     

 while True:    
             count_sem, total_gpa, credit = input_user ()   
             user_menu = menu ()

             if user_menu == 1:
                     cgpa2 = cgpa (total_gpa,count_sem) 
                     print "Total CGPA: %.2f" % cgpa2

            elif user_menu == 2: 
                    print "22222@"
             print "DONE"

0 个答案:

没有答案