2015-12-09 46 views
-1
def gpa(grades): 
    total = 0 
    if grades == 'A': 
     total += 4 
    if grades == 'B': 
     total += 3 
    if grades == 'C': 
     total += 2 
    if grades == 'D': 
     total += 1 
    if grades == 'F': 
     total += 0 
    for x in grades: 
     return points = sum(points)/len(grade) 
+1

你需要展示它是如何不工作。將錯誤消息或錯誤的輸出複製到您的問題中。 –

回答

1

您應該提供更多關於您希望代碼執行的內容以及它爲什麼不起作用。

不過,我認爲下面的代碼會適合你。

def gpa(grades): 
    total = 0 
    for grade in grades: 
     if grade == 'A': 
      total += 4 
     if grade == 'B': 
      total += 3 
     if grade == 'C': 
      total += 2 
     if grade == 'D': 
      total += 1 
     if grade == 'F': 
      total += 0 
    return round(float(total)/float(len(grades)),1) 

def main(): 
    print gpa({'A','B','C','D'}) 

if __name__ == "__main__": main() 

輸出: 2.5