←

Python Choice Case & Menu-Driven Programs

Learn how to design interactive menu programs, choice-based calculators, nested delivery logic, and Matplotlib data visualization.

Author: By Instructor

1. What is Choice Case / Menu-Driven Programming?

A Choice Case / Menu-Driven Program presents a numbered menu of options to the user, takes the user's selected choice, and executes the corresponding code branch using if-elif-else or match-case.

Easy English Explanation

  • User Control: User selects one option from a list (e.g. 1, 2, 3).
  • Interactive Flow: Execution branches dynamically based on choice.
  • Common Use Cases: ATM machines, Calculators, Order Menus, Game options.

Hinglish Explanation

  • Choice Case ka Matlab: User multiple options me se koi ek option select karta hai.
  • Execution: Jo choice user type karta hai, wahi code branch execute hota hai.
  • Use Cases: Menu programs, Calculators, Billing systems me mainly use hota hai.

2. Example 1: Beverage Selection Menu

Python Code - Beverage Selector
choice = 1

if choice == 1:
    print("You selected Tea")
elif choice == 2:
    print("You selected Coffee")
elif choice == 3:
    print("You selected Juice")
else:
    print("Invalid choice")
Output
You selected Tea

3. Example 2: Choice-Based Calculator

Python Code - Calculator with Operations & Operator Display
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")

choice = 1
a = 15
b = 5

if choice == 1:
    print("you have choosen = (+ , a+b)")
    print("Result =", a + b)
elif choice == 2:
    print("you have choosen = (- , a-b)")
    print("Result =", a - b)
elif choice == 3:
    print("you have choosen = (* , a*b)")
    print("Result =", a * b)
elif choice == 4:
    print("you have choosen = (/ , a/b)")
    print("Result =", a / b)
else:
    print("Invalid choice")
Output
you have choosen = (+ , a+b)
Result = 20

Line-by-Line Breakdown

Line Code Snippet Easy English Explanation Hinglish Explanation
1-4 print("1. Addition"...) Displays numbered options menu to user. User ko choices dikhane ke liye menu print karta hai.
6-8 choice, a, b Takes user choice and two input numbers for calculation. User ki selected choice aur calculation ke liye 2 numbers lene ka setup.
10-12 if choice == 1: print(...) If choice is 1, prints operator notification and calculates sum a + b. Agar choice 1 hai to addition ka result (a + b = 20) print karta hai.

4. Example 3: Choice-Based Even / Odd Checker

Python Code - Choice Even/Odd
print("1. Check Even")
print("2. Check Odd")

choice = 1
num = 14

if choice == 1:
    if num % 2 == 0:
        print("Even number")
    else:
        print("Not even")
elif choice == 2:
    if num % 2 != 0:
        print("Odd number")
    else:
        print("Not odd")
else:
    print("Invalid choice")
Output
Even number

5. Example 4: Maximum or Minimum of Two Numbers

Python Code - Max/Min Selector
print("1. Maximum of two numbers")
print("2. Minimum of two numbers")

choice = 1
a = 25
b = 40

if choice == 1:
    if a > b:
        print(a, "is maximum")
    else:
        print(b, "is maximum")
elif choice == 2:
    if a < b:
        print(a, "is minimum")
    else:
        print(b, "is minimum")
else:
    print("Invalid choice")
Output
40 is maximum

6. Real-Life Problem: E-Commerce Delivery Charge Calculator

Rules: Purchase amount must be greater than 150. Based on item count (≤ 5 or ≥ 6) and delivery speed choice (1 = Same Day, 2 = Next Day), delivery charges are calculated dynamically.

Python Code - Delivery Charge Calculator
amount = 200.0
items = 4
days = 1

if amount > 150:
    if items <= 5:
        if days == 1:
            deliver_charge = 8.0
            total_cost = deliver_charge + amount
            print(f"Total cost is {total_cost}")
        elif days == 2:
            deliver_charge = items * 1.5
            total_cost = deliver_charge + amount
            print(f"Total cost is {total_cost}")
    elif items >= 6:
        if days == 1:
            deliver_charge = items * 2.50
            total_cost = deliver_charge + amount
            print(f"Total cost is {total_cost}")
        elif days == 2:
            deliver_charge = items * 1.20
            total_cost = deliver_charge + amount
            print(f"Total cost is {total_cost}")
    else:
        print("Invalid input")
else:
    print("Err: purchase amount is less than 150")
Output
Total cost is 208.0

7. Data Visualization: Matplotlib Bar Graph Introduction

Python's matplotlib.pyplot library allows plotting bar graphs using plt.bar(x, y).

Python Code - Matplotlib Bar Graph
import matplotlib.pyplot as plt

students = ["Aman", "Rahul", "Priya", "Simran", "Gagan"]
marks = [75, 85, 65, 90, 80]

plt.bar(students, marks)

plt.title("Student Marks")
plt.xlabel("Students")
plt.ylabel("Marks")

plt.show()

Line-by-Line Breakdown

Line Code Snippet Easy English Explanation Hinglish Explanation
1 import matplotlib.pyplot as plt Imports plotting module with alias plt. Graph banana ke liye matplotlib library import ki.
6 plt.bar(students, marks) Plots vertical bars for each student's marks. Har student ke marks ke liye vertical bar chart banata hai.
8-10 plt.title(), xlabel(), ylabel() Adds title and axis labels to the chart. Graph ka main title aur X/Y axis labels set karta hai.

8. Summary & Key Points

Term Meaning Primary Use Case
Choice User's selected numeric or string option Menu-driven user input
Menu System Numbered list of options displayed to user Calculators, Food ordering, Banking menus
Implementation Built using if-elif-else or match-case Branching control flow dynamically