2013-05-19 19 views
0

有人能告訴我我的代碼有什麼問題嗎?我必須創建成績簿應用程序

  1. 在控制檯上顯示所有學生及其成績。你的清單應該包括一個指定類名,章節和分級權重的標題。
  2. 計算所有學生的成績,並在控制檯上顯示這些信息以及學生的字母等級(請注意,學生成績是加權的!)。
  3. 計算平均成績爲班級和字母等級爲類

代碼:

f=open("greades.txt", "r") 
sum=0 
for line in f: 
    #split data into rows 
    items = line.split() 
    sum=0 
    #getting the data in rows 
    for i in range(1,9): 
     sum+=int(items[i]) 
    print(items[0],"\tTotal:",sum,"\tAverage:",sum/9) 


    def average(mygreades): 
    """ Function to calculate the average of an input of the List that i have """ 
    if(len(mygreades)==0): 
     return 0.0 
    mygreades=[1] 
    sum=0 
    for item in mygreades: 
     sum+=item 

    avg = sum/len(mygreades) 
    return avg 

def converter(mygreades): 
    """ Function will convert an input list of strings to a number list """ 
    numberList = [] 
    for item in mygreades: 
     if(item.isnumeric()): 
      numberList.append(eval(item)) 
    return numberList 


main() 
+0

你遇到了什麼問題?爲什麼這個標籤C? – JJJ

+0

你會得到什麼,爲什麼它是錯的 – octoback

+0

它不在IDEL運行 – user2351884

回答

1

你有一個壓痕錯誤。考慮下面的代碼行:

def average(mygreades): 
""" Function to calculate the average of an input of the List that i have """ 
if(len(mygreades)==0): 

注意如何doc字符串和if語句中def下正是一字排開。這是不正確的縮進 - 函數的主體必須縮進。像在這裏做的那樣,在內有一個定義爲的函數也是有點不尋常的。

+0

你看錯了方向(我認爲)。該函數不在循環內部,'def'被錯誤地縮進,否則平均值的定義返回0.0或None。 – Ben

+1

@Ben:你在分裂頭髮。你可以說def是在for循環中,def的主體是不正確的,或者你可以說body是正確的,但def是錯誤的。無論哪種方式,由於代碼存在於問題中並由解釋器看到,def在for循環中。重要的是解釋器如何解釋它,在這種情況下,def是在for循環中。 –