2017-10-13 57 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) 
相关问题