2017-10-13 56 views
0

我找不出爲什麼我的代碼不工作,非常沮喪。我經常得到錯誤:int對象沒有要追加的屬性(對於average.append(i,average // 250))。但我無法弄清楚這裏到底出了什麼問題。是否可以在追加函數中導入其他定義? 我希望有人能幫助我! 我一般代碼的任何幫助表示讚賞:)Python錯誤:int對象沒有要追加的屬性?

def main(): 
    average = [] 
    y_values = [] 
    for x in range(0, 2501, 500): 
     for i in range(250): 
      average.append(calculate(x)) 
     average = sum(average) 
     print("{} euro, {} worpen".format(i, average//250)) 
     y_values.append(average//250) 

    x_values = [0, 500, 1000, 1500, 2000, 2500] 
    y_values = [] 
    plt.plot(x_values, y_values) 
    plt.xlabel("Startgeld") 
    plt.ylabel("Aantal worpen") 
    plt.title("Monopoly") 
    plt.show() 

def calculate(game_money): 
    piece = monopoly.Piece() 
    board = monopoly.Board() 
    owns = possession(board) 
    dice = throw() 
    throw_count = 0 
    number = 0 
    total_throw = 0 

    while not all(owns.values()): 
     number == throw() 
     piece.move(number) 
     total_throw = total_throw + number 
     throw_count += 1 

     if total_throw > 40: 
      game_money += 200 

     elif board.values[piece.location] > 0: 
      if game_money > board.values[piece.location]: 
       if owns[board.names[piece.location]] == False: 
        owns[board.names[piece.location]] = True 
        game_money = game_money - board.values[piece.location] 
     return total_throw 

def throw(): 
    dice = randint(1,6) + randint(1,6) 
    return dice 

def possession(board): 
    owns = {} 
    for i in range(40): 
     if board.values[i] > 0: 
      owns[board.names[i]] = False 
    return owns 

if __name__ == "__main__": 
    main() 
+0

你能分享完整的輸出嗎? –

+3

你正在重新分配:'average = sum(average)'。 – Li357

+0

你最初有'平均'作爲列表。然後,將所有元素進行彙總,並將結果賦予「平均值」,現在這是一個數字。那時,你顯然不能再追加它。例如。假設它包含[1,2,3]。然後總和是數字6.你不能追加到數字6,也不能將數字6加起來。 –

回答

0

你做代碼中的小錯誤。看到我下面的評論,並相應地更正你的代碼。好運:-)

y_values = [] 
average = [] 
for x in range(0, 2501, 500): 
    for i in range(250): 
     average.append(calculate(x)) 
    #average = sum(average) #This is your mistake. Now onward average will be considered as int object make it like below 
    average1 = sum(average) 
    print("{} euro, {} worpen".format(i, average1//250)) 
    y_values.append(average1//250) 
相關問題