Python Forum
Python : NameError: name 'total_wait' is not defined
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python : NameError: name 'total_wait' is not defined
#1
I'm trying to plot the results, but I get this error:
Error:
NameError: name 'total_wait' is not defined.
I am able to print the result but unable to get the value in my graph.
May I know where I did it wrong?




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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')
Reply
#2
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....
1
plt.plot(s.total_wait)
Recommended Tutorials:
Reply
#3
I have try that but it's not working. I don't why
Reply
#4
(Jun-04-2017, 05:43 PM)carla Wrote: but it's not working
in what way? Do you get another error? Post all errors in full
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  NameError: name 'predicted_labels' is not defined hobbyist 2 2,846 Dec-08-2023, 02:18 PM
Last Post: hobbyist
  NameError: name 'rand' is not defined Truman 7 9,189 Jun-17-2020, 07:14 PM
Last Post: Truman
  NameError mlab not defined PythonAndArduino 5 8,554 Nov-08-2017, 06:47 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020