Creating a Cinema Simulator Using Python
A real-life, beginner-friendly Python program to have fun with
This is a perfect example of the combination of logic and loops in Python to create an incredibly powerful program. I will go through everything step by step so you can experience the beauty of this program with me. If you’re new to Python, this is a perfect program to open the door for you. It is meant for absolute beginners.
What is the program about? This program asks a user what movie they want to watch, and decides if they are ready to enjoy the movie by its availability, the user’s age, and the number of tickets left.
Definitions of any Python vocabulary will be listed at the bottom.
I will explain line by line.
1.films = {
2. "Avengers Endgame": [3, 5],
3. "The Prestige": [18, 5],
4. "The Bourne Ultimatum": [15, 5],
5. "Joker": [12, 5]
6.}
1 — I created a variable called films which is basically a dictionary.
2 to 6 — I created a dictionary with 4 pairs of keys and values. Each key is a string set as the name of some movie. Since each value is a list of 2 values, the first one for each is the age requirement, and the second is the number of tickets left for the specific movie.
7.while True:
8. choice = input("What movie would you like to 9.watch?").strip().title()
7 — I created a while loop with the condition True, which is one of the boolean, and since True is always True, it will make the entire program run forever.
8 — I created another variable called choice that should store the user input statement asking what movie the user would like to watch.
9 — I added 2 string methods at the end of the statement because the type of the input should be a string. The .strip() takes away any extra spaces from the input, and the .title() makes the first letter of each word in the input uppercase. Since strings are iterable, we can perform these tasks.
Note: When writing if statements, we first always assume that the condition will have a result of True, so we can keep the code going. The code that’s supposed to run when the condition is False is usually written after.
10.if choice in films:
11. age = int(input("How old are you?").strip())
10 — I created an if statement stating: if choice in films, which basically translates to: if the movie that the user gave as input is inside the films dictionary as a key…
11 — Inside the if statement, I created a variable called age, that should store the user input statement asking how old the user is. Notice how I added an int before the statement. This stands for integer, and since the age has to be given as an integer, you have to add this.
12.if age >= films[choice][0]:
13. if films[choice][1] > 0:
14. print("Enjoy the film!")
15. films[choice][1] = films[choice][1] - 1
16. print("There are" , films[choice][1] , "tickets left for 17.this movie!")
12 — I created another if statement stating that, if the age the user gave as input is greater than or equal to films[choice][0]… films[choice][0] translates to: go to the films dictionary, go to the film the user choose (the key), and then go to the 0th index of the values of that certain key, which is the 1st value in the list of values. The index represents the age requirement for that movie.
13 — If the above line is True, I created another if statement stating, if films[choice][1] is greater than 0…The index is now 1, so it’ referring to the 2nd value in the list of values for that certain key. The second value represents the number of tickets left for that movie. The line means: if the number of tickets for that movie has not run out…
14 — If the above is True, print, or display “Enjoy the film!” to the screen.
15 — We already know that films[choice][1] is the number of tickets left for that movie. This line is reassigning a value to this original value. films[choice][1] = films[choice][1]-1 translates to, take away 1 ticket from the number of tickets and put that value back into films[choice][1].
16 to 17 — In this line, we want to print, or display how many tickets are left for that specific movie. We already know that value as films[choice][1], so we just need to add that in between the print statement, and the computer will return its value automatically, along with the other string parts.
18. else:
19. print("Sorry, we are sold out!")
20. else:
21. print("You are too young to see that film!")
22.else:
23. print("We don't have that film...")
18 to 19 — I created an else statement that will run if the condition for its corresponding if statement is False. It will display to the screen, “Sorry, we are all sold out!”
20 to 21 — The else statement will display to the screen, “You are too young to see that film!”
22 to 23 — The else statement will display to the screen, “We don’t have that film…”
Note: Notice how the else statements perfectly align with the if statements in a column form. This is a way you can tell which else statement refers to which if statement. You can see this in the following: the entire program.
And we’re done. Try running the program for yourself.
films = {
"Avengers Endgame": [3, 5],
"The Prestige": [18, 5],
"The Bourne Ultimatum": [15, 5],
"Joker": [12, 5]
}
while True:
choice = input("What movie would you like to watch?").strip().title()
if choice in films:
age = int(input("How old are you?").strip())
if age >= films[choice][0]:
if films[choice][1] > 0:
print("Enjoy the film!")
films[choice][1] = films[choice][1] - 1
print("There are", films[choice][1], "tickets left for this movie!")
else:
print("Sorry, we are sold out!")
else:
print("You are too young to see that film!")
else:
print("We don't have that film...")
Feel free to add as many movies as you desire.
PROGRAM FORMATTING MIGHT NOT BE 100% ACCURATE!
Important Vocabulary:
- dictionary: https://www.pythonforbeginners.com/dictionary/how-to-use-dictionaries-in-python
- string: https://learnpythonthehardway.org/book/ex6.html
- key: explained in the dictionary link
- value: explained in the dictionary link
- variable:https://www.tutorialspoint.com/python/python_variable_types
- while loop: https://www.tutorialspoint.com/python/python_while_loop.htm
- boolean: https://www.pythonforbeginners.com/basics/boolean
- string methods: https://www.programiz.com/python-programming/strings-method
- if statement: http://anh.cs.luc.edu/handsonPythonTutorial/ifstatements.html
- index: https://www.programiz.com/pythonprogramming/methods/list/index
- print: https://pythonprogramming.net/python-tutorial-print-function-strings/
- assignment: https://infohost.nmt.edu/tcc/help/pubs/python/web/assignment-statement.html
- else statement: https://www.tutorialspoint.com/python/python_if_else.htm
For the beginners reading this: good luck in your Python programming journey! It will be a great one!
Write your mind, or read other’s. Visit The Academically Driven!
Email: theacademicallydriven@gmail.com
Instagram: @theacademicallydriven
Facebook: @theacademicallydriven
LinkedIn: The Academically Driven
Twitter: @AcademicallyThe