Why is the code (Python) giving error?

49 views Asked by At
import numpy as np

import matplotlib.pyplot as plt


class Prisoners_Dilemma:


    def __init__(self,n,p):
        self.n = n
        self.p = p


    def decision_array(self):
        self.dict_dict = {}

        for i in range(1,self.n + 1):
            self.dict_dict[i] = []
            list_list = []

            for j in range(1,self.n):        
                #np.random.seed(j)
                self.r = np.random.uniform(0,1)

                if self.r > self.p:
                    q = 0

                else:
                    q = 1

                list_list.append(q)                
                self.dict_dict[i] = list_list

        return self.dict_dict


    def payoff(self):
        self.dict_dict_2 = {}

        for i in range(1,self.n + 1):
            self.dict_dict_2[i] = []
            list_list_2 = []
            list_list_3=[]

            for j in range(1, i):
                    list_list_2.append(self.dict_dict[j][i-2])

            for j in range(i + 1, self.n + 1):
                    list_list_2.append(self.dict_dict[j][i-1])



            list_list_2_np = np.array(list_list_2)
            against_i = np.sum(list_list_2_np)
            for_i = np.sum(self.dict_dict[i]) 

            if against_i == 0 and for_i == 0:
                payoff_i = 2

            elif against_i == 0 and for_i != 0:
                payoff_i = 5

            elif against_i != 0 and for_i == 0:
                payoff_i = -5

            else:
                payoff_i = -2

            list_list_3.append(payoff_i)

            self.dict_dict_2[i]=list_list_3

        return self.dict_dict_2


    def gameplay(self, N, initial_count):

        self.counter = initial_count

        for i in range(N):
            for j in range(1, self.n + 1):
                z = self.dict_dict_2[j]
                x = np.array(z)


                self.counter += np.sum(z)
        return self.counter

y = Prisoners_Dilemma(15,0.015)

print (y.gameplay(20,100))

In the above code, the compiler gives the error that instance has no attribute as dict_dict_2 even though its prefixed with self. Moreover, it is perfectly fine with dict_dict. For the sake of completeness I have included the whole code but the problem lies only in payoff and gameplay methods?

2

There are 2 answers

2
Ignacio Vazquez-Abrams On

dict_dict_2 is only created in payoff(), therefore you must call it before attempting to call gameplay().

4
Anand S Kumar On

The issue is that you are only creating self.dict_dict_2 variable in the payoff function, but in your logic where you are calling gameplay() function , you are not calling the payoff() function before accessing dict_dict_2 , from the looks of it you are not calling that function anywhere at all.

Not sure what dict_dict_2 holds, but the above is the reason why you are getting the issue, maybe you can move the initialization part of dict_dict_2 to __init__() function , though that would not fix the complete issue, since you would still be trying to access dict_dict_1[j] which can error out if j is not a key in dict_dict_2 .