[Solved] Ticket Booking using Python

Ticket Booking: Consider there is an online site for users to book tickets to the events. And the maximum capacity of the cart is 5 (that is any user cannot book more than 5 tickets). So here we are gonna implement those cart is full and cart is empty exceptions.

Ticket Booking using Python

Write program to take a list of fixed size, if  tried to remove an already empty list or if we tried to add more elements to an already full list,  get an exception. So for  both cart is full and cart is empty exceptions should be call .

Create a cart (list) of fixed size 5. Now follow a menu-driven approach, to add or remove or display the tickets in the cart.

Note: Show message as “Cart is full”  when trying to add into a full cart. Show message as “Nothing in the cart to remove” in Custom exception when trying to remove from an empty cart. Show message as “Nothing in the cart to display” in Custom exception when trying to display an empty cart. Display “Checking out…” when the user check out and exit from the program. While removing element from the list remove the lastly added element in the list.

Refer sample input/output for other further details and format of the output.

[All Texts in bold corresponds to the input and rest are output]
Sample Input/Output 1:

1.Add ticket to Cart
2.Remove from Cart
3.Display Cart
4.Check Out
Enter your choice:
3
Nothing in the cart to display
1.Add ticket to Cart
2.Remove from Cart
3.Display Cart
4.Check Out
Enter your choice:
2
Nothing in the cart to remove
1.Add ticket to Cart
2.Remove from Cart
3.Display Cart
4.Check Out
Enter your choice:
1
Enter the ticket number to be added to the cart:
78
1.Add ticket to Cart
2.Remove from Cart
3.Display Cart
4.Check Out
Enter your choice:
1
Enter the ticket number to be added to the cart:
24
1.Add ticket to Cart
2.Remove from Cart
3.Display Cart
4.Check Out
Enter your choice:
1
Enter the ticket number to be added to the cart:
92
1.Add ticket to Cart
2.Remove from Cart
3.Display Cart
4.Check Out
Enter your choice:
1
Enter the ticket number to be added to the cart:
75
1.Add ticket to Cart
2.Remove from Cart
3.Display Cart
4.Check Out
Enter your choice:
1
Enter the ticket number to be added to the cart:
30
1.Add ticket to Cart
2.Remove from Cart
3.Display Cart
4.Check Out
Enter your choice:
1
Cart is full
1.Add ticket to Cart
2.Remove from Cart
3.Display Cart
4.Check Out
Enter your choice:
3
The tickets in the cart are:
78
24
92
75
30
1.Add ticket to Cart
2.Remove from Cart
3.Display Cart
4.Check Out
Enter your choice:
2
1.Add ticket to Cart
2.Remove from Cart
3.Display Cart
4.Check Out
Enter your choice:
2
1.Add ticket to Cart
2.Remove from Cart
3.Display Cart
4.Check Out
Enter your choice:
2
1.Add ticket to Cart
2.Remove from Cart
3.Display Cart
4.Check Out
Enter your choice:
2
1.Add ticket to Cart
2.Remove from Cart
3.Display Cart
4.Check Out
Enter your choice:
2
1.Add ticket to Cart
2.Remove from Cart
3.Display Cart
4.Check Out
Enter your choice:
2
Nothing in the cart to remove
1.Add ticket to Cart
2.Remove from Cart
3.Display Cart
4.Check Out
Enter your choice:
3
Nothing in the cart to display
1.Add ticket to Cart
2.Remove from Cart
3.Display Cart
4.Check Out
Enter your choice:
1
Enter the ticket number to be added to the cart:
25
1.Add ticket to Cart
2.Remove from Cart
3.Display Cart
4.Check Out
Enter your choice:
1
Enter the ticket number to be added to the cart:
63
1.Add ticket to Cart
2.Remove from Cart
3.Display Cart
4.Check Out
Enter your choice:
4
Checking out…

Solution:

class StackUnderFlow(Exception):
    pass

class Nothing(Exception):
    pass

class full(Exception):
    pass

l = []

while(True):
    try:
        print("1.Add ticket to Cart")
        print("2.Remove from Cart")
        print("3.Display Cart")
        print("4.Check Out")
        n = int(input("Enter your choice:\n"))

        if n == 1:
            if len(l) == 5:
                raise full()
            else:
                l.append(int(input("Enter the ticket number to be added to the cart:\n")))
        elif n == 2:
            if len(l) == 0:
                print("Nothing in the cart to remove")
            else:
                l.pop()

        elif n == 3:
            if len(l) == 0:
                print("Nothing in the cart to display")
            else:
                print("The tickets in the cart are:")
                for i in l:
                    print(i)
        elif n == 4:
            print("Checking out...")
            break
    except ValueError:
        print("Invalid choice")
    except StackUnderFlow:
        print("Cart is empty")
    except Nothing:
        print("Nothing in the cart to remove")
    except full:
        print("Cart is full")

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

πŸ“Œ Nodejs and Android 😎
πŸ“Œ Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *