Here is the layout of the cinema: see image.

The seats in the cinema have been represented in a data model using a list of lists. Each inner list is a row. Each seat in a row is marked as being either available, using integer 0 (zero), or booked, using integer 1.

cinema = [ [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ]

In this version of the data structure, seat 'A4' is marked as being booked and all the other seats are marked as available.

Task 1: BASIC SELECTION, REPETITION and USER I/O

Starting with the 'cwk1T1.py' code file, you are to develop two functions and basic user interaction to book a cinema ticket:

FUNCTION: displaySeats()

This function is required to iterate over the 'cinema' list of lists and display whether a seat is booked, using the letter X or available, using the letter O; NOT zero. It should display the seats in a tabulated format as close to the following example as possible: see image.

In this example of the output from the displaySeats() function, the seat at 'A4' has been booked. This function does not return a value. NOTE: List indexes start at 0 (zero).

FUNCTION: bookASeat(seatID)

This function assumes the user has entered a valid seat I.D. in the format: Letter (A through F) directly followed by either a single digit or two digits (1 -> 9, or 01 -> 12). The function needs to transpose the letter part of the seat I.D. to an integer ('A' = 0, B = 1, F = 5). It also needs to change the string digits to an integer (1 = 0, 2 = 1, 12 = 11). These can then be used to check if the seat in the cinema list of lists is already booked (1) or not (0). If it is not booked, then it should be changed to booked and the function should return the Boolean value True, else it should return the Boolean value False. NOTE: the function will need global access to the cinema variable.

USER INTERACTION:

This constitutes the 'main' program (where it starts). It should:

1. Call the displaySeats() function
2. Ask the user to enter the seat I.D. they would like to book
3. Ask the user to enter their age
4. Output the price of the seat
5. Ask the user if they wish to proceed with booking (Y/y/N/n)

Part 4 should be implemented as a nested set of selection statements that implement the following: see image.

Part 5 should call the bookASeat() function passing the seat I.D., if and only if, the user enters 'Y' or y. If the function returns Boolean True the program should output, Seat successfully booked, else it should output Seat already booked.

The layout of your program code should be as follows at this point: see image.

Task 2: INTERMEDIATE USER INTERACTION

This task assumes you have completed and tested your Task 1 implementation. You should start by taking a copy of your task 1 file: 'cwk1T1.py' and call it: cwk1T2.py.

This task involves the development of two further functions and updating the 'main' part of the program to implement an iterating menu system.

FUNCTION: getValidSeatID()

This function will add an element of robustness to your user interaction and stop users from attempting to book an invalid seat ID. You should start by setting a variable 'seatID' to None

The function should use a repetition construct to repeatedly ask the user for a seat ID allowing them to cancel booking a seat by typing in 'CANCEL' or cancel at any point

Within the loop, the function should check the validity of the seat ID, i.e. it correctly matches the format as described in the bookASeat() function description in Task 1 above.

If and ONLY if it does correctly match the required format, then the 'seatID' variable should be set to the seat ID entered by the user.

Else, if the user has not typed in 'CANCEL' or cancel, it should output an appropriate error message.

The loop should terminate once a valid seat ID has been entered by the user, or they have typed in 'CANCEL' or cancel and the seatID should be returned in either case.

Appropriate use of 'try' and except statements will give you the highest marks with regard to robustness.

USER INTERACTION: Menu System

Replace the code in the main section to implement an iterating menu system. DO NOT use a self-calling function to implement the iteration (looping). The menu should be displayed in each iteration and the user given the opportunity to enter their menu choice, after which appropriate code should be executed depending upon that choice. The menu displayed should be:

1. Book a seat
2. Cancel a seat booking
3. Quit

If choice '1' is chosen, the following should be executed:

  • displaySeats() function is called, then
  • The code from the 'user interaction' sub task from Task 1 should be placed here with appropriate use of the getValidSeatID() function and the bookASeat() function

if choice '2' is chosen, the following should be executed:

  • getValidSeatID() is called to get a valid seat number
  • if the seat number is not None
    • if the seat is already booked
      • Change the seat to not booked
      • output "The seat has been released"
    • else
      • output "This seat has not been booked"

if choice '3' is chosen, the following should be executed:

  • Prompt the user for 'Are you sure Y/N: and get their input
  • If their input is 'y' or Y, enable the main menu loop to naturally terminate DO NOT use exit() to terminate the loop and the program

else

  • Output "Invalid menu choice"

The layout of your program code should be as follows at this point: see image.

Task 3: Permanent Changes to the Data: File Input and Output

This task assumes you have completed and tested your Task 2 implementation. You should start by taking a copy of your task 2 file: 'cwk1T2.py' and call it: cwk1T3.py.

At this point you should have noticed that the 'cinema' data structure is not affected permanently by the program, as it is hard coded into the program code. You are to replace the existing cinema data structure variable in the global variables section:

cinema = [ [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ]

With:

cinema = None

You are then to create two new functions and update the main program section to appropriately call them. You should ensure a copy of the 'cinema.dat' file (available with this specification on Canvas) is placed in the same folder as your program file.

Function: readCinemaFile()

This function needs global access to the global 'cinema' variable

The cinema variable should be initialised to an empty list

This function should open the 'cinema.dat' file for read access

If this is successful, the function should iterate reading in all of the lines of the file

  • Each line should be iterated over to create a 'row' list and each row appended to the cinema list
  • '0' (Character zero) should be stored as an integer 0 (zero) in the cinema row
  • '1' (Character one) should be stored as an integer 1 (one) in the cinema row

Appropriate use of 'try' and except statements will give you the highest marks with regard to robustness.

Function: writeCinemaFile()

This function needs global access to the global 'cinema' variable

The function should open the 'cinema.dat' file for write access - overwriting the existing file

Each row in the 'cinema' variable needs to be turned into a string of the following format:

9,9,9,9,9,9,9,9,9,9,9,9nl

The 9 represents the seat and should be set to character '0' (zero) if seat is available and character 1 if the seat is booked. The nl is a "new-line/carriage return".

Appropriate use of 'try' and except statements will give you the highest marks with regard to robustness.

Utilising the File Functions: Main program

Insert a call to the readCinemaFile() function at the beginning of the main program section; before the menu system code.

Insert a call to the writeCinemaFile() function in the menu option '1' code if the bookASeat() function returns True

Insert a call to the writeCinemaFile() function in the menu option '2' code, but only if the seat was booked and has now been released.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.