Python : NameError: name 'total_wait' is not defined - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Python : NameError: name 'total_wait' is not defined (/thread-3574.html) |
Python : NameError: name 'total_wait' is not defined - carla - Jun-04-2017 I'm trying to plot the results, but I get this error: I am able to print the result but unable to get the value in my graph. May I know where I did it wrong? import numpy as np class Simulation: def generate_interarrival(self): return np.random.poisson(1./3) def generate_service(self): return np.random.poisson(1./4) def __init__(self): self.queue = [] self.averageWaitingTime=[] self.total_wait=[] self.num_in_system = 0 self.clock = 0.0 self.t_arrival = self.generate_interarrival() self.t_depart = float('inf') self.num_arrivals = 0 self.num_departs = 0 self.total_wait = 0.0 self.averageWaitingTime= 0.0 def advance_time(self): t_event =min(self.t_arrival, self.t_depart) self.total_wait += self.num_in_system*(t_event - self.clock) print self.total_wait, 'customer total wait' self.clock = t_event if self.t_arrival <=self.t_depart: self.handle_arrival_event() else: self.handle_depart_event() self.averageWaitingTime =self.total_wait/self.t_depart print self.averageWaitingTime, ' average waiting time per customer' def handle_arrival_event(self ): self.num_in_system +=1 self.num_arrivals += 1 num_arrivals = self.clock+ self.generate_interarrival() print num_arrivals, ' customer {} interarrival'.format(i) if self.num_in_system <=1: self.t_depart = self.clock + self.generate_service() self.t_arrival = self.clock + self.generate_interarrival() def handle_depart_event(self): self.num_in_system -=1 self.num_departs += 1 num_departs = self.clock print num_departs, ' customer {} departed'.format(i) if self.num_in_system>0: self.t_depart = self.clock + self.generate_service() else: self.t_depart = float(' inf') np.random.seed(0) s = Simulation() for i in range (1000): s.advance_time() import matplotlib.pyplot as plt plt.plot(total_wait) plt.ylabel('Waiting time(min)') plt.xlabel('Number of Customers') RE: Python : NameError: name 'total_wait' is not defined - metulburr - Jun-04-2017 Quote:plt.plot(total_wait)total_plot doesnt exist, but there is a class attribute for Simulation s is your Simulation object in which has an attribute total_wait. Within a class definition you use self.total_wait, outside of the class you need to refer to it using the object, in this case 's' Quote:s = Simulation()try this.... plt.plot(s.total_wait) RE: Python : NameError: name 'total_wait' is not defined - carla - Jun-04-2017 I have try that but it's not working. I don't why RE: Python : NameError: name 'total_wait' is not defined - metulburr - Jun-04-2017 (Jun-04-2017, 05:43 PM)carla Wrote: but it's not workingin what way? Do you get another error? Post all errors in full |