Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Adding Final Touches to the Simulation

00:00 Now that you’ve defined all that you need to define for the simulation to work, it’s time to make the main function to actually run the simulation. Here, you’re going to collect user input, run the simulation, and then print the results to the terminal. So define a main() function.

00:16 You’re going to have some setup here. I’m going to set a random seed here to 42, and if you set the same thing here, you’ll get the same results that I do.

00:26 And you’ll say num_cashiers (number of cashiers), num_servers (number of servers), and num_ushers (number of ushers), is going to equal get_user_input() to use that function that you just defined.

00:38 Now to run the simulation, we’re going to make a new env, which is going to be a simpy.Environment.

00:46 Then say env.process(). You’re going to run the theater, so pass in the env, the num_cashiers, num_servers, and the num_ushers to get all the employees in there. And then to run the environment, you’ll do env.run() and then say until=90.

01:08 And what this is referring to is the number of minutes that you run the simulation. Now, you won’t actually have to wait 90 minutes, but it will be 90 minutes within the environment for the simulation.

01:21 Okay! So now that the simulation is completed, you will want to view the results, so the mins and the secs are going to equal get_average_waitor actually, calculate_wait_times(), so let me get rid of this. And there we go, calculate_wait_times(). And you’ll pass in the wait_times.

01:49 And then from here, you can go ahead and print

01:53 "Running simulation..." And you can do some f-string formatting, so after a newline ("\n") say f"The average wait time is " and then pass in the mins, say "minutes", and then pass it in the secs, and "seconds".

02:14 And let me make sure I add some commas here

02:19 so that the print() doesn’t have an issue. And I’ll actually go up here—and yeah, I forgot commas here as well.

02:29 Okay. So the last thing you’ll want to do is make a if __name__ == '__main__':, like so, and then you’ll call main(). All right!

02:40 So if you want to go ahead and finally test this out, let’s go ahead and run this with the default parameters. So I’m going to say python simulate.py and—oh! I’ve got a SyntaxError.

02:52 Looks like an extra period on line 45. So let’s go up to line 45, and there it is. Let’s get rid of that. Save it,

03:06 and let’s try that again. Okay! So the # of cashiers working: let’s just say the default of 1. 1 server and 1 usher.

03:18 And what you should end up with is 43 minutes and 38 seconds, which—if you go back to the goal of 10 minutes or less—is way off. So there’s some work to do here.

03:29 But your simulation is working, and you just tested it out for 90 minutes and it took less than a second to run. Okay! So that’s great. In the next lesson, you’re going to start experimenting with these parameters and seeing if you can get the wait times down to below 10 minutes, as that’s what the goal is.

Become a Member to join the conversation.